views:

20

answers:

1

Hi,

I have been trying to shortern this route:
http://abc.localhost/user/view/index/id/1

to this:
http://abc.localhost/user/1

with the following portion of code in my bootstrap but I keep getting an error stating that the 'Reversed route is not specified', any ideas why?

$route = new Zend_Controller_Router_Route_Regex(
    'user/(\d+)',
    array(
        'module'        => 'user',
        'controller'    => 'view',
        'action'        => 'index'
    ),
    array(
        1 => 'id'
    )
);
$router->addRoute('user', $route);

Thanks,
Martin

+3  A: 

If you want to use the URL helper with Regex routes you need to pass a 4th parameter to Zend_Controller_Router_Route_Regex that it can use to rebuild the route. This 4th parameter should be a string in a sprintf-format which it can inject the params into.

In your case it would be something like:

$route = new Zend_Controller_Router_Route_Regex(
    'user/(\d+)',
    array(
        'module'        => 'user',
        'controller'    => 'view',
        'action'        => 'index'
    ),
    array(
        1 => 'id'
    ),
    'user/%d'
);
$router->addRoute('user', $route);

There is some info on this right at the end of the manual section on Regex routes: http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.regex - but it's easy to miss.

Tim Fountain
Fantastic that is it!! Thanks!
Martin