9

那么 - 如果我有一个可能与许多路线匹配的网址怎么办......哪条路线会赢?将分派哪个动作?

它是简单的-首先定义-首先发送的吗?

以下是路线示例:

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

这个 urlexample.com/api/v1/route1会被路由到apiRoute1apiCatchAll吗?

4

1 回答 1

19

由于附加到路由堆栈的路由存储在优先级列表中,因此第一个匹配的路由将获胜。

路线通过priority设置附加到主路线。较高的优先级意味着首先检查路线。默认情况下,读取第一个附加的路由(如果它们都具有相同的优先级或根本没有优先级)。

'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将首先匹配,因为它的优先级很高。

于 2013-02-27T21:32:12.177 回答