phalconphalcon-routing

I need some understanding of the routing of a multi module Phalcon application


I need some help with a multi module Phalcon application. I followed the instructions as per https://github.com/phalcon/mvc/tree/master/multiple but cannot get the variable routing working for the non default module.

$router = new Router();
$router->setDefaultModule("admin");
$router->setDefaultAction('index');

This works for the admin module:

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

This works only works for the api module (the not default module) when set manually:

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

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

But this won't work for the api module:

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

I then get an error like below when I use /api or /api/user:

\www\site\public\index.php:104:string 'admin\controllers\ApiController handler class cannot be loaded'

But when I access /api/user/index it works. It looks like for the not default module it forgets the setDefaultAction


Solution

  • You're missing routes with default controller, actions:

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

    Phalcon needs routes to be strictly specified, otherwise it's not going to resolve them. We're paying this price for routing's high performance.