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.

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


3.8k
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: http://docs.phalconphp.com/en/latest/reference/routing.html#short-syntax.



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