简体   繁体   中英

add a different layout

如何在配置文件中为前端添加新布局,并为管理部分保留一个布局

By default this is not possible, because the bootstrap is (obviously) executed before the request is routed. The bootstrap can not know, if the front- or back-end is called. You can create a FrontControlle -Plugin, which read a (custom) config-setting and the routed request and then set the correct Layout.

I had exactly the same problem and solved it by subclassing bootstrap. So I had three bootstrap files

  1. User space one
  2. Admin one
  3. Common one

Something like that for the bootstrap file (they can all be placed on the default Zend location)

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    protected function _initXXX() {
        /* COMMON FOR ALL */
    }

    /* ... etc ... */
}


class BootstrapAdmin extends Bootstrap
{
    protected function _initAdmin1() {
        /* specific for admin */
    }
    /* ... etc ... */
}

class BootstrapUser extends Bootstrap
{
    protected function _initUser1() {
        /* specific for admin */
    }
    /* ... etc ... */
}

Where (1) and (2) extend (3). I re-create the code if you want to.

After that in the index.php:

if ($adminMode) {
$application->setBootstrap(APPLICATION_PATH . '/Name_of_Bootstrap_file.php', 'BootstrapAdmin');
} else {
$application->setBootstrap(APPLICATION_PATH . '/Name_of_Bootstrap_file.php', 'BootstrapUser');
}

The first time I did it, I placed all the classes inside the same file Name_of_Bootstrap_file.php, in order to change them together, might do it differently next time though...

Hope I helped.

Layout that depends on what modules is loaded

in my configs/application.ini

resources.layout.layout = "default"
resources.layout.pluginClass = "Core_Controller_Plugin_ModuleBasedLayout"

And then my plugin

<?php
class Core_Controller_Plugin_ModuleBasedLayout 
    extends Zend_Layout_Controller_Plugin_Layout
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $this->getLayout()->setLayoutPath( 
            Zend_Registry::get('config')->resources->frontController->moduleDirectory
            . DIRECTORY_SEPARATOR . $request->getModuleName() . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'layouts' );
    }
} 

and in my bootstrap.php

    protected function _initConfig() {
    Zend_Registry::set ( 'config', new Zend_Config ( $this->getOptions () ) );
}

My layouts is stored in views/layouts/default.php

Thought I'd chime in with my solution. This assumes the "admin" section is a module.

I've developed an action helper than can switch layout based on module name and configuration - ModuleLayoutLoader

You can configure it from your config file using the associated ModuleLayout application resource plugin. For example

resources.moduleLayout.admin.layout = "admin"
resources.moduleLayout.anotherModule.layout = "foo"

You can also set the layoutPath property for your module layouts using something like this if you want to keep your layout scripts separate

resources.moduleLayout.admin.layoutPath = APPLICATION_PATH "/path/to/admin/layout"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM