Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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.



36.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() }}


23.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.