Question

I'm having issues with Zend Framework 2 Router object. I'm trying to implement Zend\Paginator into the quickstart routing mechanism example without success. Let me explain:

After following the quickstart I ended with something like this:

'album' => array(
    'type'    => 'Segment',
    'options' => array(
        'route' => '/album[/:action][/:id]',
        'constraints' => array(
            'action' => '[a-z][a-z0-9\-]*',
            'id' => '[0-9]+'
        ),
        'defaults' => array(
            'controller' => 'Album',
            'action' => 'index'
        )
    )
)

The problem arises when I tried to implement a paginator for my browse/index page. Giving that I need a router like /album[/:action][/:id][/:page] that doesn't work because the router assigns :page var to :id. I removed the :id section ending with something like /album[/:action][/:page] and invoking in my add/edit action

$matches = $this->getEvent()->getRouteMatch();
$id = $matches->getParam('page');

working without problems but I like things keep mnemonic. So my question is: Is it possible to achieve this without creating a duplicate router only for the paginator to look like I like? an I doing something incorrect?

Was it helpful?

Solution

It's not unique enough, as an example with a route:

/album[/:action][/:id][/:page]

id and page are optional, and both can be integers

if we try to match this route:

/album/index/99

the 99 could match id, with page not being present, or also page, with id not being present. there's no way to know which is which, so this will not work.

You could add in an identifier, something like this:

/album[/:action][/id/:id][/:page]

so then you would have a literal in there to make sure you know it's an id:

/album/index/id/99 # :id = 99

or /album/index/99 # :page = 99

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top