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

How to work with annotations router in multi module applications?

My codeļ¼š $di = new \Phalcon\DI\FactoryDefault(); $di->set('router', function() { $router = new \Phalcon\Mvc\Router\Annotations(false); $router->setDefaultModule("frontend"); $router->addResource('Products', '/api/products'); return $router; }); $application = new \Phalcon\Mvc\Application();

$application->setDI($di);
$application->registerModules(array(
    'frontend' => array(
        'className' => 'Frontend\Module',
        'path' => '../apps/frontend/Module.php'
    )
));
   echo $application->handle()->getContent();

Fatal error: Phalcon\Mvc\Router\Annotations::handle(): The argument is not iterable() in /var/www/html/public/index.php on line 47



98.9k
Accepted
answer

Since the annotations are read from the controller classes when the routes are processed, is necessary use a global autoloader to make these classes available for the router:

$loader = new Phalcon\Loader();

$loader->registerNamespaces(array(
   'Application\Backend\Controllers' => '../app/backend/controllers/',
   'Application\Frontend\Controllers' => '../app/frontend/controllers/',
))->register();

Also, 1.0.0 provide better support for routing with annotations in namespaces/modules:

$di->set('router', function() {
    $router = new \Phalcon\Mvc\Router\Annotations(false);
    $router->addModuleResource('frontend', 'Application\Backend\Controllers\Products', '/api/products');
    return $router;
});

Check out this complete example: https://gist.github.com/phalcon/5000377