zend-frameworklayoutmodularzend-layout

Can i have multiple layouts in Zend Framework?


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


Solution

  • 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