i am developing a Rest Controller with Zend and i am confused with the mapping of urls to the Router.

Basically i read about Zend Router and i could not plan my urls in order to satisfy the mentioned routes.

These are some of my urls that should be mapped to Routers.

  1. http://localhost/api/v1/tags.xml

  2. http://localhost/api/v1/tags.xml?abc=true (param: abc=true)

  3. http://localhost/api/v1/tags/123456.xml (param: 123456.xml)

  4. http://localhost/api/v1/tags/123456/pings.xml (params: 123456, pings.xml)

  5. http://localhost/api/v1/tags/123456/pings.xml?a=1&b=2 (params: 123456, pings.xml, a=1, b=2)

  6. http://localhost/api/v1/tags/123456/pings/count.xml (params: 123456, pings, count.xml)

I am planning such that for the url patterns 1 to 3, "tags" should be the controller and for the url patterns 4 to 6, "pings" should be the controller.

Now i am unsure about how to configure the routers such that the above scenarios will work. Note that i cannot change these urls. I can offer 100 of my reputation score to the good answer.

Comments

i can vote you up

Written by experimentX

Why do you have/need "public/index.php" at the start of all of these URLs?

Written by Tim Fountain

They are not mandatory and i removed them. I am only worried about the routing of these urls to their respective controllers and actions, passing the needed parameters.

Written by dskanth

Try to be consistent, and your routes will appear clearly. > "tags" should be the controller and for the url patterns 4 to 6, "pings" should be the controller. < Makes no sense to me.

Written by Boris Guéry

All i wanted is that, the requests should route to different controllers for patterns 1 to 3 and 4 to 6. In general scenario, all the above requests will route to "api" controller (if the module is default), but that would make the process hectic. So i am looking for a way to route the requests to different controllers. How can i achieve this?

Written by dskanth

Accepted Answer

First two URLs can be combined to one router.

$r = new Zend_Controller_Router_Route_Regex('api/v1/tags.xml',
                array('controller' => 'tags', 'action' => 'index'));
$router->addRoute('route1', $r);

To differentiate the first two routes, check for the presence of the abc parameter in your tags controller. Add the following in your tags controller, index action.

if($this->_getParam('abc') == "true")
{
//route 2
} else {
// route 1
}

Similarly, routes 4 and 5 can be combined into one route.

I have explained for Route 6. For route 3, you can use the same logic.

$r = new Zend_Controller_Router_Route_Regex('api/v1/tags/(.*)/pings/(.*)',
                array('controller' => 'pings', 'action' => 'index'),
array(1 => 'param1',2=>'param2')
);
$router->addRoute('route6', $r);

The parameters can then accessed like the following in pings controller.

$this->_getParam('param1') and $this->_getParam('param2')

For Route 5 :

$r = new Zend_Controller_Router_Route_Regex('api/v1/tags/(.*)/pings.xml',
                array('controller' => 'pings', 'action' => 'index'),
array(1 => 'param1')
);
$router->addRoute('route5', $r);

The parameters (part of the URL after ?) will not be handled in the Router. By default, they will be passed to your controller.

To get a specifc parameter value passed in your URL, use the following in your controller.

$this->_getParam('a');

The logic is use (.*) in your route and assign them a parameter name and access them in your controller

Written by emaillenin
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki