views:

271

answers:

1

I've been googling around and I can't seem to find anything which explains the use of ZF router well. I've read the documentation on the site, which seems to only talk about re-routing.

I am trying to make the format: /module/value/controller/action give /module/controller/action passing on value as a parameter

e.g. /store/johnsmithbigsale/home/newstuff would route to /store/home/newstuff passing on johnsmithbigsale as the value to a parameter with a hidden namespace e.g. storeName.

Some help would be greatful!

+2  A: 

You can use Zend_Controller_Router_Route to map your url parts to modules, controllers, actions, and parameters that can be used in the controller by $this->_getParam('varName'). You can define these routes in the application.ini file or in the application bootstrap.

// custom city route
$route = new Zend_Controller_Router_Route(
    'cities/:city',
    array(
        'controller' => 'city',
        'action'     => 'view'
    )
);
$this->addRoute('city', $route);

// custom buy widgets route
$route = new Zend_Controller_Router_Route_Regex(
    'buy_(.+)_widgets/([0-9]+)(.*)',
    array(
        'controller' => 'widgets',
        'action'     => 'view'
    ),
    array(
        1 => 'nothing',
        2 => 'widget_id',
        3 => 'vars'
    )
);
$this->addRoute('widgets', $route);

The regex route is kind of specific to my app, but you can see that each match can get mapped to a parameter.

delsurf
I posted some sample code. Hope it helps...
delsurf
Sweet, that made sense!
cappuccino