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

Routing problem in multimodule project

Hi, my aplication is divided in 2 modules frontend and admin and I'm using this as a router:

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

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

$router->add('/admin/confirm/{code}/{email}', array( "module" => 'admin', 'controller' => 'user_control', 'action' => 'confirmEmail' ));

$router->add('/admin/reset-password/{code}/{email}', array( "module" => 'admin', 'controller' => 'user_control', 'action' => 'resetPassword' ));

The problem I'm facing is that I want to use "/admin" to show the andmin module but i keep getting that when i hit this route(/admin/confirm/bBm9xWmyip2QTRj5ia3n4M9xF8Ps0wd/[email protected]):

object(Phalcon\Mvc\Dispatcher\Exception)#68 (7) { ["message":protected]=> string(83) "habitat25\Frontend\Controllers\UserControlController handler class cannot be loaded" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(2) ["file":protected]=> string(45) "/var/www/html/habitat24_adri/public/index.php" ["line":protected]=> int(37) ["trace":"Exception":private]=> array(3) { [0]=> array(4) { ["function"]=> string(23) "_throwDispatchException" ["class"]=> string(22) "Phalcon\Mvc\Dispatcher" ["type"]=> string(2) "->" ["args"]=> array(2) { [0]=> string(83) "habitat25\Frontend\Controllers\UserControlController handler class cannot be loaded" [1]=> int(2) } } [1]=> array(4) { ["function"]=> string(8) "dispatch" ["class"]=> string(18) "Phalcon\Dispatcher" ["type"]=> string(2) "->" ["args"]=> array(0) { } } [2]=> array(6) { ["file"]=> string(45) "/var/www/html/habitat24_adri/public/index.php" ["line"]=> int(37) ["function"]=> string(6) "handle" ["class"]=> string(23) "Phalcon\Mvc\Application" ["type"]=> string(2) "->" ["args"]=> array(0) { } } } ["previous":"Exception":private]=> NULL }

It's trying acces the frontend module and I don't know why. Anybody any hint about what's happening?



1.4k

Woops! I just added the namespaces in the routes and now it works!

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

$router->add("/admin/:controller/:action/:params", array( 'namespace' => 'habitat24\admin\Controllers', 'module' => 'admin', "controller" => 1, "action" => 2, "params" => 3 ) );

$router->add('/admin/confirm/{code}/{email}', array( 'namespace' => 'habitat24\admin\Controllers', "module" => 'admin', 'controller' => 'user_control', 'action' => 'confirmEmail' ));

$router->add('/admin/reset-password/{code}/{email}', array( 'namespace' => 'habitat24\admin\Controllers', "module" => 'admin', 'controller' => 'user_control', 'action' => 'resetPassword' ));

edited Oct '15

Look at https://github.com/phalcon/mvc/tree/master/multiple . Do you set namespace in Module.php? If you set namespace in Module.php you don't need set namespace in routes. Also you can use group of router https://docs.phalcon.io/en/latest/reference/routing.html#groups-of-routes - it's good practice. Group allow write common params of router once: namespace, module.