简体   繁体   English

Zend Framework:如何将默认布局脚本更改为layout.phtml以外的其他内容?

[英]Zend Framework: How do I change the default layout script to something other than layout.phtml?

I'd like to name my default layout file something other than layout.phtml, since it doesn't really describe what type of layout it is. 我想将我的默认布局文件命名为layout.phtml之外的其他东西,因为它并没有真正描述它的布局类型。 How can I do this? 我怎样才能做到这一点? Thanks! 谢谢!

From your Bootstrap.php file, you could do something like this: 从您的Bootstrap.php文件中,您可以执行以下操作:

protected function _initLayoutName()
{
    // use sitelayout.phtml as the main layout file
    Zend_Layout::getMvcInstance()->setLayout('sitelayout');
}

If you want to use a different layout for a different module, you need to register a plugin in the Bootstrap and have the plugin contain the following code: 如果要为不同的模块使用不同的布局,则需要在Bootstrap中注册插件并使插件包含以下代码:

class Application_Plugin_LayoutSwitcher extends Zend_Controller_Plugin_Abstract
{
    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
    {
        $module = $request->getModuleName(); // get the name of the current module

        if ('admin' == $module) {
            // set the layout to admin.phtml if we are in admin module
            Zend_Layout::getMvcInstance()->setLayout('admin');
        } else if ('somethingelse' == $module) {
            Zend_Layout::getMvcInstance()->setLayout('somethingelse');
        }
    }
}

From within your application.ini, you can do this to set the layout script: 在application.ini中,您可以执行此操作来设置布局脚本:

resources.layout.layout = "layoutname"

This will not work on a per layout basis, however. 但是,这不适用于每个布局。 If you need to change the layout based on the module, you will have to use a plugin, but you can use the setting in application.ini to set the default layout name. 如果需要根据模块更改布局,则必须使用插件,但可以使用application.ini中的设置来设置默认布局名称。

If you want to have a specific layout depending on based on your modules you can create a plugin and register it in your boostrap : 如果你想根据你的模块有一个特定的布局,你可以创建一个插件并在你的boostrap中注册它:

<?php

class Plugin_LayoutModule extends Zend_Controller_Plugin_Abstract 
{
        /**
         * preDispatch function.
         * 
         * Define layout path based on what module is being used.
         */
        public function preDispatch(Zend_Controller_Request_Abstract $request)
        {
                $module = strtolower($request->getModuleName());
                $layout = Zend_Layout::getMvcInstance();

                if ($layout->getMvcEnabled())
                {
                        $layout->setLayoutPath(APPLICATION_PATH . '/modules/' . $module . '/layouts/');
                        $layout->setLayout($module);
                }
        }
}

//Register it in your bootstrap.php     
    <?php
        defined('APPLICATION_PATH')
            or define('APPLICATION_PATH', dirname(__FILE__));
        ...

        Zend_Layout::startMvc();
        $frontController->registerPlugin(new Plugin_LayoutModule()); 
    ?>

EDIT : 编辑:

to set your layout to another file using .ini file : 使用.ini文件将布局设置为另一个文件:

create layout.ini file and put in it : 创建layout.ini文件并输入:

[layout]
layout = "foo"
layoutPath = "/path/to/layouts"
contentKey = "CONTENT"

in your bootstrap file : 在你的bootstrap文件中:

$config = new Zend_Config_Ini('/path/to/layout.ini', 'layout');

$layout = Zend_Layout::startMvc($config);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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