Zend Framework routes with ajax friendly parameters

I am creating REST API with Zend Framework. I have a route setup that maps to all the resources that I have modeled. I also created a RESTful controller plugin to route different types of requests (PUT, GET, etc.) to the correct controller action.

I am making ajax calls with jQuery and by default add GET parameters to the url for the ajax call. I want to pass these parameters as filters to my resources.

How do I get Zend Framework to pass these parameters to my controllers? Hoping not 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

      

0


a source to share


3 answers


changing Lighttpd rewrite rule to this fix:

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

0


a source


You might want to modify your Ajax calls to force parameters to be sent to the script:



$.get("/catalog/", { max_results: "20", start_index: "5" } );
$.post("/catalog/", { max_results: "20", start_index: "5" } );

      

0


a source


You can access these parameters using:

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

      

in your controller action. And for your convenience, to access these parameters, you can use:

$array = parse_url($params, PHP_URL_QUERY);

      

I guess you don't need a special route to make this work. Hope this helps you.

0


a source







All Articles