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

Help ! Something for Router...

I build four modules: Admin Home User Home ,and an Addons Then I create four router:

    $router->add("/index/:controller/:action/:params", array(
        'module'     => 'index',
        'controller' => 1,
        'action'     => 2,
        'params'     => 3,
    ));
    $router->add("/admin/:controller/:action/:params", array(
        'module'     => 'admin',
        'controller' => 1,
        'action'     => 2,
        'params'     => 3,
    ));
    $router->add("/home/:controller/:action/:params", array(
        'module'     => 'home',
        'controller' => 1,
        'action'     => 2,
        'params'     => 3,
    ));
    $router->add("/user/:controller/:action/:params", array(
        'module'     => 'user',
        'controller' => 1,
        'action'     => 2,
        'params'     => 3,
    ));

if users input: Home/...or User/... or Index/... or Admin/... ,it will redirect to their modules,But users input anywords which is not in 'home,admin,user,index',it will redirect allows:

$router->add("**....**/:params.", array(
    'module'     => 'user',
    'controller' => 'addons',
    'action'     =>'excut',
    'addons'  =>'',(this is the head word)
    'params'     => 1,
));

How to write the last router-string???Please help me



2.1k
Accepted
answer
//services
$di['router'] = function () {

    $router = new Router();
    $router->setDefaultModule('index');
    $router->removeExtraSlashes(true);
    return $router;
};

//bootstrap
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
    ));
}