I know with ZF1 you would retrieve the module/controller name using custom View Helpers that would get the singleton frontController object and get the name there.
Using ZF2 as they've abolished alot of the singleton nature of the framework and introduced DI where I've specified aliases for all of my controllers within this module... I can imagine I would get it through accessing the DI or perhaps injecting the current name into the layout.
Anyone got any idea how you would do it. I guess there a hundred different ways but after sniffing about the code for a few hours I can't really figure out how its meant to be done now.
The reason I wanted the controller name is to add it to the body as a class for specific controller styling.
Thanks, Dom
This would be a solution I got to work with zf2 beta5
module/MyModule/Module.php
module/MyModule/src/MyModule/View/Helper.php
In any viewfile
I wanted to access current module/controller/route name in navigation menu partial and there was no way but to implement custom view helper and access it, i came up with the following, i am posting it here.
In order to access the values in layout/view here is how i do it.
Hope this helps someone.
instead of extending
onBootStrap()
inModule.php
, you can usegetViewHelperConfig()
(also inModule.php
). The actual helper is unchanged, but you get the following code to create it:Short Code here :
ZF2 is out and so is the skeleton. This is adding on top of the skeleton so it should be your best example:
Inside Module.php
The actual ViewHelper:
Inside any of your views/layouts
I created CurrentRoute View Helper for this purpose.
Install it:
Register module in config/application.config.php:
Use it in any view/layout file:
You can see full documentation and code https://github.com/tasmaniski/zf2-current-route