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

Annotation runner and module registration with prefix

I am using the annotation runner and when I add a prefix it seems to fall back to the default routing pattern.

   $router = new Phalcon\Mvc\Router\Annotations();
   $router->addModuleResource('frontend', 'Foo\Frontend\Controllers\Index', '/foo');
   $di->set('router', $router);
/**
 * @RoutePrefix("/bar")
 */
class IndexController extends ControllerBase
{

    /**
     * @Get("/")
     */
        public function indexAction()
        {
            echo "foo";
        }

}

https://example.com/foo/bar/ gives me php Phalcon\Mvc\Dispatcher->_throwDispatchException(FooController handler class cannot be loaded, 2)

Is there any obvious error in my code? I have tried multiple variants and it works without have the prefix in addModuleResource.



98.9k

The prefix in your app is different ( @RoutePrefix("/bar")) than the one you're using to register the resource: addModuleResource('frontend', 'Foo\Frontend\Controllers\Index', '/foo')



2.6k

ok, only if they are equal it works. But then I dont understand the point to allow the prefix in addModuleResource? If I remove the @RoutePrefix annotation, it fails. What I am looking for is to load the modules internal routing under a prefix. Can that be done the way I wanted to do it?



2.6k

is the purpose of the prefix in the addModuleResource to enclose files for the annotations runner (performance win) and not to prepend the route regex?



98.9k

The purpose of addModuleResource is to tell the router that is must load the annotations in the class just if the current URI starts by the prefix specified there, this allows to avoid reading controllers and their annotations without need.



2.6k

thanks phalcon. Also thanks for the great work. I will submit an NFR for this. Maybe it would be useful for more people.