Zend - routing rules based on parameters number

265 Views Asked by At

in my project I'm using zend to handle routing. Atm we've got routing rules which looks like this: array( 'match' => 'page', 'params' => array('page', 'idConfiguration'), 'controller' => 'controler1', 'action' => 'action1' )

So we access this action by: http://base_url/page/1/1223324 for example.

Is there a simple solution to create rules so i can determine which action is called based on number of params?

I'd like it to look the following way: http://base_url/ - action 0 http://base_url/pageNumber - action 1 http://base_url/pageNumber/idConfiguration - action 2 http://base_url/pageNumber/idConfiguration/someotherparam - action 3

Thank you in advance for help

2

There are 2 best solutions below

0
bububaba On BEST ANSWER

Ini-based solution (hope I understand what you want):

routes.action0.route = "/:pageNumber"
routes.action0.defaults.controller = "controller0"
routes.action0.defaults.action = "action0"
routes.action0.reqs.pageNumber = "\d+"

routes.action1.route = "/:pageNumber/:idConfiguration"
routes.action1.defaults.controller = "controller1"
routes.action1.defaults.action = "action1"
routes.action1.reqs.pageNumber = "\d+"
routes.action1.reqs.idConfiguration= "\d+"

routes.action2.route = "/:pageNumber/:idConfiguration/:someOtherParam"
routes.action2.defaults.controller = "controller2"
routes.action2.defaults.action = "action2"
routes.action2.reqs.pageNumber = "\d+"
routes.action2.reqs.idConfiguration= "\d+"
routes.action2.reqs.someOtherParam = "someOtherRegEx"
0
El Barto On

You can subclass Zend_Controller_Router_Route and create the routing behaviour you like. Of the top of my head, and without testing it you can try something like this:

class MyRoute extends Zend_Controller_Router_Route
{
    public function match($path) 
    {
        $result = array(
            'controller' => 'index',
            'action' => substr_count($path, '/'),
        );

        return $result;
    } 
}

You would need to add validations, of course. Also you should return FALSE if the URL doesn't match and you want it to be tested with other routes. But this should give you a general idea on how to work this out.