Adam Youngers
Adam Youngers

Reputation: 6749

ZF2 Routing Multiple Controllers based on route constraints

I'm refactoring some routing in a project and I'm trying to retain the current path structure of the following...


/events <-- Works

/events/super-bowl <-- Works

/events/2012-super-bowl <-- Doesn't work! Archive Layout

/events/2012-super-bowl/detail-page <-- Doesn't work! Archive sub layout

/events/2018-super-bowl <-- Works. Standard Layout, no sub layout


This is what I have tried...

'router' => [
    'routes' => [
        'events' => [
            'type'    => 'literal',
            'options' => [
                'route'    => '/events',
                'defaults' => [
                    'controller' => 'events',
                    'action'     => 'index',
                ],
            ],
            'may_terminate' => true,
            'child_routes' => [
                'super-bowl' => [
                    'type'    => 'segment',
                    'options' => [
                        'route'       => '/super-bowl',
                        'defaults'    => [
                            'action' => 'superBowl',
                        ],
                    ],
                ],
                'super-bowl-archives' => [
                    'type'    => 'segment',
                    'options' => [
                        'route'       => '/[:year]-super-bowl[/:detail]',
                        'constraints' => [
                            'year' => '^(2012|2013|2014)',
                        ],
                        'defaults'    => [
                            'controller' => 'super-bowl-archives',
                            'action' => 'index',
                        ],
                    ],
                ],
                'super-bowl-standard' => [
                    'type'    => 'segment',
                    'options' => [
                        'route'       => '/[:year]-super-bowl',
                        'constraints' => [
                            'year' => '\d{4}'
                        ],
                        'defaults'    => [
                            'controller' => 'super-bowl-standard',
                            'action' => 'index',
                        ],
                    ],
                ],

            ],
        ],
    ],
],

I'm struggling with the archive layouts. I'd like to capture certain years and point them to a different controller. The archives have a sub route as well that I'm not sure how to achieve.

Upvotes: 0

Views: 90

Answers (1)

Ermenegildo
Ermenegildo

Reputation: 1308

There are two problems in that configuration.

First: matching rule.

Since the matching isn't completely regex based, the correct "pattern" for year is '2012|2013|2014'

Second: matching order.

According to Zend router documentation:

Routes will be queried in a LIFO order, and hence the reason behind the name RouteStack

Last route will be matched first. Since 2012 is matched with \d{4}, you have to first test those "exceptions".

Simply put super-bowl-archives route after super-bowl-standard and it'll work

'router' => [
    'routes' => [
        'events' => [
            'type' => 'literal',
            'options' => [
                'route' => '/events',
                'defaults' => [
                    'controller' => 'events',
                    'action' => 'index'
                ]
            ],
            'may_terminate' => true,
            'child_routes' => [
                'super-bowl' => [
                    'type' => 'segment',
                    'options' => [
                        'route' => '/super-bowl',
                        'defaults' => [
                            'action' => 'superBowl'
                        ]
                    ]
                ],
                'super-bowl-standard' => [
                    'type' => 'segment',
                    'options' => [
                        'route' => '/[:year]-super-bowl',
                        'constraints' => [
                            'year' => '\d{4}'
                        ],
                        'defaults' => [
                            'controller' => 'super-bowl-standard',
                            'action' => 'index'
                        ]
                    ]
                ],
                'super-bowl-archives' => [
                    'type' => 'segment',
                    'options' => [
                        'route' => '/[:year]-super-bowl[/:detail]',
                        'constraints' => [
                            'year' => '2012|2013|2014'
                        ],
                        'defaults' => [
                            'controller' => 'super-bowl-archives',
                            'action' => 'index'
                        ]
                    ]
                ]
            ]
        ]
    ]
],

Upvotes: 1

Related Questions