vatzec
vatzec

Reputation: 403

ZF2 - Two routes with overlapping patterns

my question regards Zend Framework 2. I have two routes that overlap a bit - one is in the form of:

/announcements/index[/:type][/:status]

and the other:

/announcements[/:action][/:id]

They are defined in configuration in the same order as above.

The purpose is that almost all of my routes have the same pattern, that is action + ID, but for the index page, which displays a list of items, I want filtering possibilities - I want the user to be able to filter announcements by type and their status (accepted/rejected/awaiting moderation). The problem is that for some reason the router selects the second route as the active one when I go to /announcements or /announcements/index. What is the best idea to solve that?

Thanks.

Upvotes: 0

Views: 101

Answers (1)

AlexP
AlexP

Reputation: 9857

Child routes is the way to go, however you can make your life easier by defining the special cases with their own route.

In my example the 'index' child route is defined before the 'default' meaning that the router would match this route first.

'announcements' => array(
    'type' => 'Literal',
    'options' => array(
        'route' => '/announcements',
        'defaults' => array(
            'controller' => 'Application\Controller\Announcements',
            'action' => 'index'
        )
    ),
    'may_terminate' => true,
    'child_routes' => array(

        'index' => array(
            'type' => 'Segment',
            'options' => array(
                'route' => '/index[/:type][/:status]',
                'defaults' => array(
                    'controller' => 'Application\Controller\Announcements',
                    'action' => 'index',
                ),
            ),
            'may_terminate' => true,
        ),

        'default' => array(
            'type' => 'segment',
            'options' => array(
                'route' => '/:action/:id',
                'defaults' => array(
                    'controller' => 'Application\Controller\Announcements',
                ),
            ),
            'may_terminate' => true,
        ),

    ),
),

Upvotes: 3

Related Questions