1

I am new with zend and I have a problem changing my layout in the bootstrap. I want to change my layout when the user is logged in.

My function to change the layout in the bootstrap is like this:

protected function _initAuthState()
{
$layout = new Zend_Layout;
$layout->setLayoutPath('/layouts/scripts');

if (Zend_Auth::getInstance()->hasIdentity()):
// Logged in.
$layout->setLayout(layout2);

else:
// Not Logged in.
$layout->setLayout(‘layout’);
endif;
}

This code doesn't work, the layout is always the same ... help!

Siguza
  • 21,155
  • 6
  • 52
  • 89
Jose Rego
  • 380
  • 5
  • 10

2 Answers2

4

You are modifying a new layout instance, not the instance that is being used by the system.

I assume you are specifying your layout params in application.ini. So you need:

$this->bootstrap('layout');
$layout = $this->getResource('layout');

Then perform your check/modification on this layout instance.

BTW, changing layout is often done using a front-controller plugin. Still runs early enough to do the job, but is often more configurable and re-usable. See here and here for two examples.

David Weinraub
  • 14,144
  • 4
  • 42
  • 64
  • thanks for the answer. I create the plugin but its giving me an error when i call him: Fatal error: Class 'Plugin_Layout' not found in /home/zerego/application/Bootstrap.php on line 8 – Jose Rego Apr 26 '11 at 16:33
  • This is a common issue related to how you invoke/instantiate your plugin. Usually, you need to set appnamespace and the prefix/folder mapping for your resource loader. If you post the code and/or config you are using to instantiate the plugin, I'll comment on the tweak required. We're almost there. ;-) – David Weinraub Apr 26 '11 at 21:40
1

I found the answer!!

this is my final result, and is working!!

Bootstrap.php:

<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    public function _initLoader(){
        $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
        'basePath'  => '../application/',
        'namespace' => 'My',
        ));

        $resourceLoader->addResourceTypes(array(
            'plugin' => array(
                'path'      => 'plugins/',
                'namespace' => 'Plugin',
            )
        ));
    }

    public function _initPlugins()
    {
    $front = Zend_Controller_Front::getInstance();
    $front->registerPlugin(new My_Plugin_Layout());
    }
}

application/plugins/Layout.php:

<?php
class My_Plugin_Layout 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('layout2');
                break;

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

            default:
                $layout->setLayout('layout');
                break;
        }
    }
}
?>
Jose Rego
  • 380
  • 5
  • 10