Can I have multiple layouts in the Zend Framework?

I have a flashy page with image rotators in the client interface.

For the back-end, I want to have a different layout. Can I have multiple layouts?

A slight hint would be noticeable.

+3
source share
4 answers

I create a layout plugin to switch layouts when a custom 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'
            )
        );

Custom layouts are included in the folder with the name 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
+6
source

Yes, you can have multiple layouts, although switching them based on a query is not so straightforward.

, - , .

ModuleLayout

ModuleLayoutLoader

+1

try

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

It will switch the layout to layoutName.phtml in the folder of your module / scripts;)

+1
source

It is not right. Line:

class MyApplication_Layout_Controller_Plugin_Layout extends end_Layout_Controller_Plugin_Layout

should be extends Zend_Controller_Plugin_Abstract. Otherwise, you will receive an error message mvcSuccessfulActionOnly.

+1
source

Source: https://habr.com/ru/post/1777282/


All Articles