We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

[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?



125.8k
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

        ]
    );