Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

[solved] Trouble with catch-all route

Hi all,

My routing currently looks like this:

$Router->add(
        '/{name}/',
        [
             'controller' =>   'store'
            ,'action'     =>   'index'
        ]
    );


    $Router->add(
        '/{name}/checkout/',
        [
            'controller'  =>   'store'
            ,'action'     =>   'checkout'
        ]
    );

    $Router->add(
        '/{name}/information/',
        [
             'controller' =>   'store'
            ,'action'     =>   'information'
        ]
    );

I have some more store-specific routes to add though, and I'd rather not add a different route for each route when I can just add a catch-all. I thought this should work:

$Router->add(
        '/{name}/',
        [
             'controller' =>   'store'
            ,'action'     =>   'index'
        ]
    );

$Router->add(
        '/{name}/{:action}/',
        [
            'controller'  =>   'store'
            ,'action'     =>   1
        ]
    );

But I keep getting a route not found exception. For a URL such as /test/information/ I get the controller name being "test" (Boo) and the action name being "information" (Yay).

Why is the controller name being overwritten?



53.4k
Accepted
answer

There must have been some problem with using {name}. Updating my second rule to the following works:

$Router->add(
        '/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?',
        [
            'controller'  => 'store',
            'action'      => 2,
            'name'            => 1

        ]
    );