Controller specific layout in ZendFramework 2

2019-05-21 11:13发布

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.

3条回答
你好瞎i
2楼-- · 2019-05-21 12:02

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

<?php
namespace MyModule;

use Zend\ModuleManager\ModuleManager;

class Module
{
    public function init(ModuleManager $moduleManager)
    {
        $sharedEvents = $moduleManager->getEventManager()->getSharedManager();
        $sharedEvents->attach(__NAMESPACE__, 'dispatch', function($e) {
            // This event will only be fired when an ActionController under the MyModule namespace is dispatched.
            $controller = $e->getTarget();
            $controller->layout('layout/alternativelayout');
        }, 100);
    }
}

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.

查看更多
在下西门庆
3楼-- · 2019-05-21 12:02

@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:

<?php
namespace MyModule;

use Zend\ModuleManager\ModuleManager;

class Module
{
    public function init(ModuleManager $moduleManager){
        $sharedEvents = $moduleManager->getEventManager()->getSharedManager();
        $sharedEvents->attach(__NAMESPACE__, 'dispatch', function($e) {
                $controller = $e->getTarget();
                if ($controller instanceof Controller\AltLayoutController) {
                    $controller->layout('layout/alternativelayout');
                }
            }, 100);
    }

I

查看更多
Summer. ? 凉城
4楼-- · 2019-05-21 12:09

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:

public function differentLayoutAction()
{
    // Use a different layout
    $this->layout('layout/different');

    return new ViewModel();
}

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.

查看更多
登录 后发表回答