不同控制器的不同布局文件 [英] Different layout file for different controller

查看:71
本文介绍了不同控制器的不同布局文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使我的ZF2模块为特定控制器加载其他布局文件?

How to make my ZF2 module load other layout file for specific controller ?

考虑到您的ZF2应用程序模块中有IndexControllerAdminController,并且IndexController正在使用 layout.phtml ,但是您想使用 adminlayout.phtml 用于AdminController.

Consider you have IndexController and AdminController in your ZF2 application module and the IndexController is using layout.phtml but you want to use adminlayout.phtml for AdminController.

怎么可能?

推荐答案

class Module {
    public function onBootstrap($e) {
        $em  = $application->getEventManager();

        $em->attach(MvcEvent::EVENT_DISPATCH, function($e) {
            $controller = $e->getTarget();
            if ($controller instanceof Controller\AdminController) {   
                $controller->layout('layout/layoutadmin.phtml');
            } else {
                $controller->layout('layout/layout.phtml');
            }   
        });
    }
}

并且不要忘记通过在模块配置文件中添加此配置来注册新控制器:

and don't forget to register your new controller by adding this config in your module config file:

'controllers' => array(
    'invokables' => array(
        'Application\Controller\Index' => 'Application\Controller\IndexController',
        'Application\Controller\Admin' => 'Application\Controller\AdminController',
    ),
),

这篇关于不同控制器的不同布局文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆