Zend框架。如何将选项传递给自定义前端控制器插件?

时间:2011-01-06 04:45:00

标签: zend-framework zend-application

我有自定义前端控制器插件,需要一些选项。 这时我在application.ini文件中加载它(插件),如下所示:

resources.frontController.plugins.DynamicLayout = "My_Controller_Plugin_DynamicLayout"

此时我只有option.ini文件,然后使用zend_config导入它。 有没有办法从ZEND的主application.ini文件中指定插件选项? 也许这样的事情?:

resources.frontController.plugins.DynamicLayout.test = "test_value"

1 个答案:

答案 0 :(得分:1)

我使用类似的东西使用bootstrap将信息传递给我的布局。

此示例适用于在不同域上运行的应用程序,因此布局不同。 (并且有一个单独的MSIE版本)。每个域作为单独的application.ini

<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

    protected function _initAutoload() {
        return new Zend_Application_Module_Autoloader(array(
            'namespace' => '',
            'basePath' => APPLICATION_PATH
        ));
    }

    // <snip> more _initMethods: Loggers, ACLs, ViewHelpers, etc. </snip>

    /**
     * Setup dynamic layout plugin
     *
     * @return Zend_Controller_Plugin_Broker
     */
    protected function _initFrontControllerLayoutPlugin() {
        // Ensure the front controller is initialized
        $this->bootstrap('FrontController');
        $front = $this->getResource('FrontController');

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

        // Set our Front Controller Plugin
        // !!!! right here I pass values to the layout
        // !!!! example layoutName, but you could pass anything you want...
        $plugin = new Plugin_DynamicLayout($layout, $this->getOption('layoutName'));

        return $front->registerPlugin($plugin);
    }

}

布局处理程序:

<?php

class Plugin_DynamicLayout extends Zend_Controller_Plugin_Abstract {

    private $layoutName;

    public function __construct(Zend_Layout $layout, $layoutName) {
        $this->layout = $layout;
        $this->layoutName = $layoutName;
    }

      public function preDispatch(Zend_Controller_Request_Abstract $request) {

        $layoutName = $this->layoutName;

        if (false !== strpos($request->getHeader('User-Agent'), 'MSIE')) {
            $layoutName = $layoutName . '-ie';
        }

        $this->layout->setLayout($layoutName);

    }
}

application.ini:

[production]
layoutName = "Some_File_Name"