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

Strange routing behavior...

Hello,

I'm new to phalcon and I'm asking myself if this behavior is "usual" - to me it looks like a bug:

/**
 * Registering a router
 */
$di['router'] = function() {

    $router = new Router();

    $router->setDefaultModule("frontend");
    $router->setDefaultNamespace("Myspace\Frontend\Controllers");
    $router->addGet("/api1/:action", array(
        'module' => 'frontend',
        'controller' => 'api',
        'action' => 1
    ));
    $router->addGet('/', array(
        'module' => 'frontend',
        'controller' => 'index',
        'action' => 'index'
    ));
    return $router;
};

When the first route is called with correct parameters, I receive the following error mesesage:

  • Myspace\Frontend\Controllers\Api1Controller handler class cannot be loaded

Which is correct, because I expect "ApiController" to be loaded instead of "Api1Controller"(see documentation: https://docs.phalcon.io/en/latest/reference/routing.html#parameters-with-names ). Am I missing something? Can't believe routing is faulty at this level.



8.1k

You have not set autoloading namespace in autoloader, may be. Autoloader can be set globally. And you have not determine action in first route also, but it's secondary obstacle.



8.6k

Autloading was setup correctly. I have now added an action entry to the array (and updated the source code from my first post to reflect this), but this doesn't change anything. Autoloading is setup properly because when I change

$router->addGet("/api1/:action/$latlngPattern/$latlngPattern/$latlngPattern/$latlngPattern", array(

to

$router->addGet("/api/:action/$latlngPattern/$latlngPattern/$latlngPattern/$latlngPattern", array(

the proper action + controller is called (\Myspace\Frontend\Controllers\ApiController).