We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 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();

    '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


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();

   'Application\Backend\Controllers' => '../app/backend/controllers/',
   'Application\Frontend\Controllers' => '../app/frontend/controllers/',

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