I have a module in my zendframework 2 application which contains two controllers. I want to set a different layout for one of the controller's actions. Is there a way to set it inside module config file?
P.s: I just tried to set it inside controller's __CONSTRUCT method using the following commands but it just didnt worked!
$event = $this->getEvent();
$event->getViewModel()->setTemplate('layout/MYLAYOUT');
But if i use the above commands inside each action of my controller it just works fine.
I can just point you into the right direction, since currently i'm unable to open a sample project. Evan Coury has posted a method for Module specific layouts. See the following links:
Module Specific Layouts in Zend Framework 2
Now how would this help you?: Well,
$controller
should have both the called controller and action stored. I'm sure you can check the$controller
for the called action and then assign the layout accordingly.I'm sorry i can currently only hint you into the direction, but i'm sure this can get you started.
@Sam's answer pretty much answers the question. As stated it just needs a check on which controller is called, which can be done like this:
I
See akrabat's examples for a number of nice ways that layouts, views, etcetera can be tweaked easily.
Specifically what you're looking for can be found on his github here.
Here is a cut-paste of the controller's action method that sets/uses the alternate layout:
Edit: It looks like akrabat has an example that says
Change the layout for every action within a module
, which might give the best pointers for setting the layout in the config; but I just had a look at the code, and the example is currently unfinished, it's not changing the layout.