Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZF2 - what will be dispatched if router matches multiple routes?

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?

Here's routes for example:

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

Would this url example.com/api/v1/route1 be routed to apiRoute1 or apiCatchAll?

like image 629
agent_smith Avatar asked Feb 27 '13 21:02

agent_smith


1 Answers

Since routes attached to the route stack are stored in a priority list, the first matched route will win.

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

In this example, route-test1 will be matched first because of its high priority.

like image 82
Ocramius Avatar answered Oct 02 '22 13:10

Ocramius