ZF2 - 如果路由器匹配多条路由,将分派什么? [英] ZF2 - what will be dispatched if router matches multiple routes?
问题描述
那么 - 如果我有一个可能与许多路由匹配的 url 怎么办……哪条路由会获胜?将分派哪个动作?
So - what if I have a url that might be matched against many routes... which route will win? Which action will be dispatched?
是否简单-先定义-先调度?
Is it simple- first defined - first dispatched?
以下是路线示例:
'route-catchall' => array(
'type' => 'regex',
'options' => array(
'regex' => '/api/v1/.*',
'defaults' => array(
'controller' => 'IndexController',
'action' => 'apiCatchAll',
),
),
),
'route-test1' => array(
'type' => 'literal',
'options' => array(
'route' => '/api/v1/route1',
'defaults' => array(
'controller' => 'IndexController',
'action' => 'apiRoute1',
),
),
),
这个 url example.com/api/v1/route1
会被路由到 apiRoute1
或 apiCatchAll
吗?
Would this url example.com/api/v1/route1
be routed to apiRoute1
or apiCatchAll
?
推荐答案
由于附加到路由堆栈的路由存储在 优先级列表,第一个匹配的路由将获胜.
Since routes attached to the route stack are stored in a priority list, the first matched route will win.
路由通过 priority
设置.更高的优先级意味着首先检查路由.默认情况下,读取第一个附加路由(如果它们都具有相同的优先级或根本没有优先级).
Routes are attached to the main route with a priority
setting. Higher priority means the route is checked first. By default, the first attached route is read (if they all have same priority or no priority at all).
'route-catchall' => array(
'type' => 'regex',
'options' => array(
'regex' => '/api/v1/.*',
'defaults' => array(
'controller' => 'IndexController',
'action' => 'apiCatchAll',
),
),
'priority' => -1000,
),
'route-test1' => array(
'type' => 'literal',
'options' => array(
'route' => '/api/v1/route1',
'defaults' => array(
'controller' => 'IndexController',
'action' => 'apiRoute1',
),
),
'priority' => 9001, // it's over 9000!
),
在这个例子中,route-test1
将首先匹配,因为它的优先级很高.
In this example, route-test1
will be matched first because of its high priority.
这篇关于ZF2 - 如果路由器匹配多条路由,将分派什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!