ZF2 Routing Multiple Controllers 基于路由约束

ZF2 Routing Multiple Controllers based on route constraints

我正在重构项目中的一些路由,我试图保留以下的当前路径结构...


/事件 <-- 作品

/events/super-bowl <-- 有效

/events/2012-super-bowl <-- 不起作用!存档布局

/events/2012-super-bowl/detail-page <-- 不起作用!存档子布局

/events/2018-super-bowl <-- 有效。标准布局,无子布局


这是我试过的...

'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',
                        ],
                    ],
                ],

            ],
        ],
    ],
],

我正在努力处理存档布局。我想捕捉某些年份并将它们指向不同的控制器。档案也有一个子路线,我不知道如何实现。

那个配置有两个问题。

第一个:匹配规则。

由于匹配不完全基于正则表达式,因此年份的正确 "pattern" 是 '2012|2013|2014'

第二:匹配顺序

根据Zend router documentation

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

最后一条路线将最先匹配。由于 2012 与 \d{4} 匹配,因此您必须首先测试那些 "exceptions".

只需在 super-bowl-standard 之后放置 super-bowl-archives 路由即可

'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'
                        ]
                    ]
                ]
            ]
        ]
    ]
],