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

currently dispatching route name

I use custom routes which include namespace besides controller and action. So for ACL purposes I use MVC route name as ACL resource name. Now I need to obtain currently DISPATCHING route name. The only solution I've come up with is to get namespace/controller/action from Dispatcher and iterating over all the routes find an appropriate one.

Is there any easiest way to obtain currently dispatching (not just matched) route name?



7.9k
edited Mar '14

Here is my config:

$router->add('/admin/:controller/:action', array(
            'controller' => 1,
            'action' => 2,
            'template' => "admin",
        ));

and in the controller I can get the template param:

$template = $this->dispatcher->getParam("template");


8.4k

Hm, so if you can't easily obtain route name let's do not use it? No, this is lame!



11.8k

+1 for add getActiveRouteName.

edited Mar '14

use:

echo $di->get('router')->getMatchedRoute()->getName()


11.8k

thx Boston! You a my hero!



8.4k

Well, Boston, you completely missed the point. Not MATCHING route (everyone can read manual, lol), but DISPATCHING. There's a huge difference!



11.8k

dV, I use on beforeExecuteRoute, and it help for me. Can you please explain the difference?

@dV this is not possible, the dispatcher does not know about routing. And action may be caused by a dispatcher->forward.



8.4k

@boston Well, it's possible to obtain currently dispatching route name by iterating through all the routes and compare route controller/action against current controller/action (taken from dispatcher).

@xAockd It's useful when you don't want to make an http redirect but instead make an inner redirect, e.g. dispatch another controller/action. For instance I use it in Security plugin on beforeDispatch to substitute current page with user login page (if it's needed). In other words I display login form without making an HTTP redirect.