且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在ZEND 2中为新模块设置新布局?

更新时间:2023-11-29 23:21:10

我最近找到了一种方法(其他人的解决方案). 将此添加到您的module.config.php(在我的情况下,该模块称为Album,它基于ZF2的演示应用程序):

I have recently found a way to do it (someone else's solution). Add this to your module.config.php (in my case the module is called Album, this is based on the demo application of ZF2):

'module_layouts' => array(
    'Album' => 'layout/layout.phtml'
),

需要在主Module.php文件中进行其他必要的更改(在此添加此onBootstrap方法并根据需要进行编辑):

The other necessary change needs to be done in the main Module.php file (add this onBootstrap method there and edit as may be needed):

public function onBootstrap($e) {
    $e->getApplication()
            ->getEventManager()
            ->getSharedManager()
            ->attach('Zend\Mvc\Controller\AbstractController', 'dispatch', 
                    function($e) {
                        $controller = $e->getTarget();
                        $controllerClass = get_class($controller);
                        $moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\'));
                        $config = $e->getApplication()->getServiceManager()->get('config');
                        if (isset($config['module_layouts'][$moduleNamespace])) {
                            $controller->layout($config['module_layouts'][$moduleNamespace]);
                        }
                    }, 100);
}