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

Get Controller and Action name in a Volt Template?

Hi there,

When you're inside a Volt template, apart from setting variable(s) inside the controller, is there any way to discover what Controller/Action has triggered the rendering of the template?

Thanks.



58.4k
Accepted
answer

Hi

I think you use method getControllerName() in volt(inside tag php) or you can create variable in controller then volt call variable it

you can access the router di from inside the volt template

        {{ router.getControllerName() }}
        {{ router.getActionName() }}


38.8k

Thanks Guys. Really appreciate your answers :)

A little more info: if you've forwarded the request from one action to another, it seems that the router doesn't know about it - it will still tell you the original. But the dispatcher knows.

Example: I have a DashboardController and the indexAction reads some info about the user and forwards to another action according to a role, like this:

$this->dispatcher->forward(array('controller' => 'dashboard', 'action' => 'dash_teamlead'));

In the dash_teamlead.volt, if I call

{{ router.getActionName() }}

, it returns blank (which means index). But if I call

{{ dispatcher.getActionName() }}

I get the correct forwarded action.