views:

882

answers:

3

I'm building a REST API with Zend Framework. I have routes setup that are mapped to all the resources I modeled. I also created a RESTful controller plugin to direct the different types of requests (PUT, GET, etc..) to the right controller action.

I'm making ajax calls with jQuery and by default it appends GET parameters to the URL for the ajax call. I want to pass along these parameters as filters on my resources.

How can I get Zend Framework to pass these parameters into my controllers? Hoping not to have to write a controller plugin to make this work...

Working URL

http://myapp.com/catalog/products/categories/max_results/20/start_index/5

What I Want

http://myapp.com/catalog/products/categories/?max_results=20&start_index=5

Zend Framework Route

routes.catalog_product_categories.type = "Zend_Controller_Router_Route"
routes.catalog_product_categories.route = "catalog/products/categories/*"
routes.catalog_product_categories.defaults.controller = "categories"
routes.catalog_product_categories.defaults.action = "productcategories"
routes.catalog_product_categories.defaults.RESTful = true
A: 

Maybe you may want to change your Ajax calls to force the parameters to be sent to the script:

$.get("/catalog/", { max_results: "20", start_index: "5" } );
$.post("/catalog/", { max_results: "20", start_index: "5" } );
MC
was hoping this would do the trick but unfortunately no dice. I tried the following in the controller but the params didn't show up anywhere: $get_params = $this->getRequest()->getParams(); $get_query = $this->getRequest()->getQuery(); $get_uri = $this->getRequest()->getRequestUri(); $get_uri_params = parse_url($get_uri, PHP_URL_QUERY); $get_raw_body = $this->getRequest()->getRawBody();
rawberg
A: 

You can access that parameters using:

$params = $this->getRequest()->getRequestUri();

on your controller action. And for your convenience to access that parameters, you can using:

$array = parse_url($params, PHP_URL_QUERY);

I think you don't need custom route to make this work. Hope this can help you.

alifity
this method would work if I was going to go the route of developing a controller plugin to manually parse all the parameters. even when I put it into an array it just turns everything after the ? in the url into one string, so I would still have to manually sift out each parameter and value.
rawberg
A: 

changing the Lighttpd rewrite rule to this fixes it:

url.rewrite-once = (
".*\?(.*)$" => "/index.php?$1",
".*\.(js|ico|gif|jpg|png|css)$" => "$0",
"" => "/index.php"
)

http://framework.zend.com/issues/browse/ZF-2901?focusedCommentId=20029#action_20029

rawberg