Results 1 to 2 of 2

Thread: Layout Changing according to Module change

  1. #1
    anindya is offline Junior Member
    Join Date
    Sep 2009
    Posts
    9

    Default Layout Changing according to Module change

    I would like to develop Module based application.
    My directory structure

    /project
    ---/application
    ------/configs
    ---------application.ini
    ------/layouts
    ---------/scripts
    admin.phtml
    usermanagement.phtml
    ------/modules
    ---------/admin
    ------------/controllers
    IndexController.php
    ------------/models
    ------------/views
    ---------------/filters
    ---------------/helpers
    ---------------/scripts
    /index
    index.phtml
    ---------/usermanagement
    ------------/controllers
    ---------------UserController.php
    ------------/models
    ------------/views
    ---------------/filters
    ---------------/helpers
    ---------------/scripts
    ------------------/user
    login.phtml
    ---------Bootstrap.php
    ---/library
    ------/Zend
    ------/ZendX
    ------/Tp
    ---------/Controller
    ------------/Action
    ---------------/Helper
    ------------------LayoutLoader.php
    ---/public
    ------/img
    ------/css
    ------/js
    ------.htaccess
    ------.index.php

    Here is my application.ini file

    [production]

    ; Config Errors
    phpSettings.display_startup_errors = 0
    phpSettings.display_errors = 0

    ; Config Timezone
    phpSettings.date.timezone = "Europe/London"

    ; Config Include Paths
    includePaths.library = APPLICATION_ROOT "/library"

    ; Config Bootstrap
    bootstrap.path = APPLICATION_PATH "/bootstrap.php"
    bootstrap.class = "Bootstrap"

    ; Config Module
    resources.modules[] = ""
    resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
    resources.frontController.moduleControllerDirector yName = "controllers"

    resources.frontController.defaultModule = "usermanagement"
    resources.frontController.defaultControllerName = "user"
    resources.frontController.defaultAction = "login"

    ; Config Layout
    resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
    resources.layout.layout = usermanagement

    admin.resources.layout.layout = admin
    usermanagement.resources.layout.layout = hospital

    ; Config View
    resources.view.encoding = "UTF-8"
    resources.view.doctype = "XHTML1_STRICT"
    resources.view.contentType = "text/html;charset=utf-8"
    resources.view.headTitle = "Test Project"
    resources.view.setSeparator = " - "

    ; Config Namespaces
    autoloadingNamespaces.tp= "Tp_"

    ; Config Database
    resources.db.adapter = "PDO_Mysql"
    resources.db.params.hostname = "localhost"
    resources.db.params.username = "root"
    resources.db.params.password = "testpassword"
    resources.db.params.dbname = "test_project"

    [developmentroduction]

    ; Config Errors
    phpSettings.display_startup_errors = 1
    phpSettings.display_errors = 1

    ; Config Database
    resources.db.params.dbname = "test_project_dev"

    And here is my Bootstrap.php file

    <?php
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /**
    * Description of Bootstrap
    *
    * @author Programmer1
    */
    class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
    {
    protected function _initAutoload()
    {
    $moduleLoader = new Zend_Application_Module_Autoloader(array(
    'namespace' => '',
    'basepath' => APPLICATION_PATH . '/modules/usermanagement'
    ));
    }

    protected function _initView()
    {
    $options = $this->getOptions();

    // Create View Object
    if (isset($options['resources']['view'])) {
    $view = new Zend_View($options['resources']['view']);
    } else {
    $view = new Zend_View;
    }

    // Set Doctype
    if (isset($options['resources']['view']['doctype'])) {
    $view->doctype($options['resources']['view']['doctype']);
    }

    // Set Content Type
    if (isset($options['resources']['view']['contentType'])) {
    $view->headMeta()->appendHttpEquiv('Content-Type',
    $options['resources']['view']['contentType']);
    }

    // Set Head Title
    if (isset($options['resources']['view']['headTitle'])) {
    $view->headTitle($options['resources']['view']['headTitle']);
    }

    // Set Separator
    if (isset($options['resources']['view']['setSeparator'])) {
    $view->headTitle()->setSeparator($options['resources']['view']['setSeparator']);
    }

    $view->addHelperPath('ZendX/JQuery/View/Helper/', 'ZendX_JQuery_View_Helper');

    $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelp er(
    'ViewRenderer'
    );
    $viewRenderer->setView($view);

    Zend_Controller_Action_HelperBroker::addHelper(new Tp_Controller_Action_Helper_LayoutLoader());
    }
    }
    ?>

    Here is LayoutLoader.php file

    <?php

    class Tp_Controller_Action_Helper_LayoutLoader extends Zend_Controller_Action_Helper_Abstract
    {

    public function preDispatch()
    {
    $bootstrap = $this->getActionController()->getInvokeArg('bootstrap');
    $config = $bootstrap->getOptions();
    $module = $this->getRequest()->getModuleName();
    if (isset($config[$module]['resources']['layout']['layout'])) {
    $layoutScript = $config[$module]['resources']['layout']['layout'];
    $this->getActionController()
    ->getHelper('layout')
    ->setLayout($layoutScript);
    }
    }
    }

    Question 1:

    Usermanagement is the default Module and login is the default Action. Different Module has different layout and whenever user switch from one module to another, the application automatically load the module's layout. But my existing application is not working like that. What are the additional tasks i have to do?

    Question 2:

    If application needs to switch different layout within same module and same controller but different action, what are the additional tasks i have to do?

  2. #2
    Gecko is offline Junior Member
    Join Date
    Jul 2009
    Posts
    4

    Default

    This is the way that I have module specific layouts in one my projects, it might be of some use to you:

    (application.ini)
    Code:
    # Layout initialisation resource
    resources.Zend_Application_Resource_Layout.options.layoutPath = APPLICATION_PATH "/views/layouts"
    resources.Zend_Application_Resource_Layout.options.pluginClass = "Project_Module_Layout_Plugin"

    Code:
    class Project_Module_Layout_Plugin extends Zend_Layout_Controller_Plugin_Layout
    {
    	public function preDispatch(Zend_Controller_Request_Abstract $request)
    	{		       
            $moduleLayoutPath =  APPLICATION_PATH . '/modules/'
                              . $request->getModuleName()
                              . '/views/layouts/';
                              
            $layout = $this->getLayout();
            
            if(file_exists($moduleLayoutPath . $layout->getLayout() . '.' . $layout->getViewSuffix())) {
            	$layout->setLayoutPath($moduleLayoutPath);
            }
    	}
    }
    So for any requested module its going to look for the layout within /modules/[moduleName]/views/layouts/layout.phtml by default.


    As to your second question, within any action you can call:

    Code:
    $this->_helper->layout->setLayout('otherlayout');
    which will use /views/layouts/otherlayout.phtml

Similar Threads

  1. How to change the layout of the error controller?
    By ponchorage in forum Model-View-Controller (MVC)
    Replies: 2
    Last Post: 06-18-2010, 04:56 PM
  2. Change Layout according to the Module
    By mm.sour in forum Model-View-Controller (MVC)
    Replies: 1
    Last Post: 02-10-2010, 09:57 AM
  3. change module views
    By mech7 in forum Model-View-Controller (MVC)
    Replies: 1
    Last Post: 06-24-2009, 04:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •