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

\Phalcon\Mvc\Router\Annotations usage

Is it reading annotations of controller only when request is coming? If it so - bad...

I have next situation:

  1. Modules with annotations in controllers.
  2. In development mode or when router cache is empty - i must load all controllers (path them to loader) and use $router->addModuleResource() to provide them into router.
  3. After that router is saved into cache
  4. When by next request router is loading from cache - it has resource definitions, but not routes of this resources (i don't want to load all classes again)

So, is there a way that to store controllers route when $router->addModuleResource() is calling (read them at moment, when function called, not per request)?



98.9k
Accepted
answer

addModuleResource has a third parameter:

public Phalcon\Mvc\Router\Annotations addModuleResource (string $module, string $handler, [string $prefix])

The prefix is a common prefix all the routes in that resource have, the router checks if the current URI starts with that prefix and loads the resource improving performance. However if you're naming the routes and the route isn't loaded when a link is generated from them, you'll got an exception.

Another important thing is using a persistent adapter for annotations like Class Phalcon\Annotations\Adapter\Files or Class Phalcon\Annotations\Adapter\Apc, these adapters avoid the permanent parsing of annotations also remove the dependency of having the real controllers to load the routes.

Ok, Thanks. First doesn't fit for controllers in modules, when you don't know what modules will have your app. But Thanks for second advise!