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

Router setDefaults can't work?

In Index i had define:

<?php ......

$di->setShared('router', function() {

$router = new \Phalcon\Mvc\Router();

$router->add('/:module/:controller/:action/:params', [
    'module'        => 1,
    'controller'    => 2,
    'action'        => 3,
    'params'        => 4,
]);
$router->setDefaultAction('index');
return $router;

});

..............

$application = new \Phalcon\Mvc\Application($di);

$application->registerModules([

'frontend' => ['path' => APPLICATION_ROOT . '/application/modules/frontend/Module.php'],

'admin' => ['path' => APPLICATION_ROOT . '/application/modules/admin/Module.php'],

'task' => ['path' => APPLICATION_ROOT . '/application/modules/task/Module.php'],

'tmp' => ['path' => APPLICATION_ROOT . '/application/modules/tmp/Module.php'],

'api' => ['path' => APPLICATION_ROOT . '/application/modules/api/Module.php'],

]);

$application->useImplicitView(true);

$application->setDefaultModule("frontend");

.....

when i visit https://xxx.com/frontend/passport/index ,it's work good, but when visit https://xxx.com/frontend/passport/ ,It's say:

Modules\Frontend\Controllers\FrontendController handler class cannot be loaded

Hi, try this:


                 $router->setDefaults(array(
                  'controller' => 'YourDefaultController',
                  'action' => 'index'
                  ));


3.8k

@PollDeveloper

Can't work too. :(

edited Jan '17

You'll have to register each level of path:

$router->add('/:module/:controller/:action/:params', [
    'module'      => 1,
    'controller'  => 2,
    'action'      => 3,
    'params'      => 4,
]);
$router->add('/:module/:controller', [
    'module'      => 1,
    'controller'  => 2,
    'action' => 'index', // this line is not necessary
]);
$router->add('/:module', [
    'module'      => 1,
    'controller'  => 'index', // this line is not necessary
    'action' => 'index', // this line s not necessary
]);


3.8k

@Ljos Bencz

Thank you.