Tag Archives: layout

How to set multiple layout in Zend FrameWork

During developing an application we need to set the multiple layout for a particular controller, action, view.

Set layout for a particular controller or action:

Within Controller/action

1
2
3
4
5
<?php   
(a) Zend_Layout::getMvcInstance()->setLayout('layout_name');
            
(b) $this->_helper->layout()->setLayout('layout-user');
?>

Within View

1
<?php $this->layout()->setLayout('layout_name'); ?>

Disable layout for a particular controller or action:

Within controller, action

1
2
3
4
<?php
$this->_helper->layout()->disableLayout(); 
$this->_helper->viewRenderer->setNoRender(true);
?>

How to set Layout using Plugin in Zend FrameWork

Some time we need to set different layout based on user type. So the best way to handle it by using plugin.

Bootstrap.php:

1
2
3
4
5
6
7
8
9
10
11
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    public function _initPlugins()
    {
        $front = Zend_Controller_Front::getInstance();
        $front->registerPlugin(new Zend_Controller_Plugin_Acl()); // Plugin file path library/zend/controller/Plugin/Acl.php
    }
}
?>

In library/zend/controller/Plugin/Acl.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
class Zend_Controller_Plugin_Acl extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch()
    {
        $user = Zend_Auth::getInstance();
        $role = $user->getIdentity()->role;
        $layout = Zend_Layout::getMvcInstance();

        switch ($role) {
            case 'admin':
                $layout->setLayout('layout1');
                break;

            case 'user':
                $layout->setLayout('layout2');
                break;

            default:
                $layout->setLayout('layout');
                break;
        }
    }
}
?>

Hope! you will enjoy.