gvlasov
gvlasov

Reputation: 20035

Parsing parameters in URLs using Zend_Controller_Router_Route

On my site I need to have URLs of pages in this manner: /category/page4, so parameter passed into a controller is a number after the word "page". I managed to get URLs in the following manner: /category/page/4 (with an extra slash) using this code:

$router->addRoute('categoryPages', new Zend_Controller_Router_Route(
    '/category/page/:page',
     array(
        'controller' => 'caegory',
        'action' => 'index'
    ),
    array(
        'page' => '\d+'
    )
));

I would expect to have URLs like /category/page4 with the following modification of this code:

// Simply removed one slash
'/category/page:page'

However, this code doesn't create routing I need. What is my mistake?

Upvotes: 0

Views: 287

Answers (1)

Tim Fountain
Tim Fountain

Reputation: 33148

You can't do this with Zend_Controller_Router_Route, named variables must sit on their own adjacent to either the url delimiter (slash), or the start/end of the route.

Instead you could do this as a regular expression route:

$router->addRoute('categoryPages', new Zend_Controller_Router_Route_Regex(
    'category/page(\d+)',
     array(
        'controller' => 'caegory',
        'action' => 'index'
    ),
    array(
        1 => 'page'
    ),
    'category/page%s'
));

Upvotes: 1

Related Questions