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

Ideas on how to make route registrations in controllers

Hi guys! First of all thank you all for this very fast framework. Coming from zf2 world I can see that is really REALLY faster :) Now my question is this: How can I register routes in my controllers without using anotations parser? I mean it looks like I am able to use initialize() method but if i register my routes there like so:

public function initialize()
{
   $this->router->add('/status-code/404', array('action' => 'code404',));
}

I'm getting:

exception 'Phalcon\Mvc\Dispatcher\Exception' with message 'Action '404' was not found on handler 'status-code''

And that means that router totally ignores new routes...

I've never personally tried setting up routes in the initialisation of a controller, but it looks like all's you're missing is setting the controller key within the router options.

Ie.

   $this->router->add('/status-code/404', array('action' => 'code404', 'controller' => __CLASS__));

Otherwise you have Phalcon trying to automatically determine what the class name of the controller would/should be, but there's no such thing as hyphenated class names in PHP ;)

Do you want to register routes IN your controllers, or FOR your controllers? As far as I know, the former cannot be done. The routes need to be initialized & checked by the app before any controller gets loaded.