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

Question about routing in bootstrap for mvc/multiple

Hoping these have quick, simple answers.

I just downloaded the mvc/multiple example from GitHub (https://github.com/phalcon/mvc/tree/master/multiple).

In the bootstrap file (public/index.php) there are 4 route mappings.

First question: Why is the 1st route mapping needed when the previous line already sets "frontend" as the default module?

Second question: Why is the 4th route needed when the 1st one should already take care of it?

Thanks in advance for the help.

Here are the route mappings straight from the sample bootstrap file:

        $router->setDefaultModule("frontend");

        $router->add('/:controller/:action', array(
            'module' => 'frontend',
            'controller' => 1,
            'action' => 2,
        ));

        $router->add("/login", array(
            'module' => 'backend',
            'controller' => 'login',
            'action' => 'index',
        ));

        $router->add("/admin/products/:action", array(
            'module' => 'backend',
            'controller' => 'products',
            'action' => 1,
        ));

        $router->add("/products/:action", array(
            'module' => 'frontend',
            'controller' => 'products',
            'action' => 1,
        ));


5.7k
Accepted
answer

These are not needed, same exact result as the original:

$router->add('/:controller/:action', array(
    // 'module' => 'frontend',
    'controller' => 1,
    'action' => 2,
));
// ...
/*
$router->add("/products/:action", array(
     'module' => 'frontend',
     'controller' => 'products',
     'action' => 1,
));
*/

I don't know why the last one is there, it could end up like the one here I suppose: https://docs.phalcon.io/en/latest/reference/routing.html#short-syntax.



2.1k
edited Jan '15

place this in your bootstrap php in public/index.php

$router = $di->get("router"); foreach ($application->getModules() as $key => $module) { $router->add('/'.$key.'/:params', array( 'module' => $key, 'controller' => 'index', 'action' => 'index', 'params' => 1 ))->setName($key); $router->add('/'.$key.'/:controller/:params', array( 'module' => $key, 'controller' => 1, 'action' => 'index', 'params' => 2 )); $router->add('/'.$key.'/:controller/:action/:params', array( 'module' => $key, 'controller' => 1, 'action' => 2, 'params' => 3 )); }

that will take care of everything. unless you have special routes.

like /login => backend, index controller, login