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

Annotations router and modules?

For example, i have modules: core, blog.

Router initialization:

$router = new \Phalcon\Mvc\Router\Annotations();
$router->setDefaultModule('core');

// some routes

$router->addModuleResource('core', 'Index', '/');

And when i trying to reach https://mysite/ i have an exception: Uncaught exception 'ReflectionException' with message 'Class IndexController does not exist'

And bootstrap (Module.php) doesn't load, code doesn't executing:

 $loader->registerDirs(array(
            $this->getModuleDirectory() . '/controllers/',
            $this->getModuleDirectory() . '/models/',
        ));

I'am doing something wrong? The idea is to load dynamically routes from annotations, and not from file or by specified code.



98.9k
Accepted
answer

The router doesn't load your modules, modules are loaded by Phalcon\Mvc\Application, so you need to register a global autoloader that allows the router to obtain the class reflection at least one time to read the annotations from the controllers.

Yeap, I knew it, Thanks!