Can i have multiple layouts in Zend Framework?

此生再无相见时 提交于 2019-12-21 19:54:05

问题


I have a flashy page with image rotators in the front end for the clients.

For back-end I want to have different layout. Can i have multiple layout?

A little hint would be appreciable


回答1:


I create a layout plugin, to switch layouts when a non-default module is called:

class MyApplication_Layout_Controller_Plugin_Layout extends Zend_Layout_Controller_Plugin_Layout
{

    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        switch ($request->getModuleName()) {
            case 'admin': $this->_moduleChange('admin');
        }
    }

    protected function _moduleChange($moduleName) {
        $this->getLayout()->setLayoutPath(
            dirname(dirname(
                $this->getLayout()->getLayoutPath()
            ))
            . DIRECTORY_SEPARATOR . 'layouts/scripts/' . $moduleName
        );
        $this->getLayout()->setLayout($moduleName);
    }

}

Then in my Bootstrap, I do this:

Zend_Layout::startMvc(
            array(
                'layoutPath' => self::$root . '/application/views/layouts/scripts',
                'layout' => 'layout',
                'pluginClass' => 'MyApplication_Layout_Controller_Plugin_Layout'
            )
        );

The non-default layouts go inside a folder named after the module, so my directory structure looks like this:

/path/to/application/views/layouts/scripts/layout.phtml --> default layout

/path/to/application/views/layouts/scripts/admin/admin.phtml --> admin layout



回答2:


Yes, you can have multiple layouts though switching them based on the request is not so straight forward.

I've had to do this enough times that I ended up developing a controller action helper and application resource plugin that you're free to use or take inspiration from.

ModuleLayout Application Resource Plugin

ModuleLayoutLoader Controller Action Helper




回答3:


try

//in controller
$this->_helper->layout->setLayout('layoutName');

It will switch layout to layoutName.phtml in your module's view/scripts folder ;)




回答4:


This is wrong. The line:

class MyApplication_Layout_Controller_Plugin_Layout extends end_Layout_Controller_Plugin_Layout

should be extends Zend_Controller_Plugin_Abstract. Otherwise, you'll get an error concerning mvcSuccessfulActionOnly.



来源:https://stackoverflow.com/questions/4296213/can-i-have-multiple-layouts-in-zend-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!