Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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



84.4k
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