开发者

Zend framework. how to pass options to custom front controller plugin?

I have custom front controller plugin that takes some options. At this time I load it (plugin) in application.ini file like this:

resources.frontController.plugins.DynamicLayout = "My_Controller_开发者_如何学JAVAPlugin_DynamicLayout"

At this time I just have option.ini file and then use zend_config to import it. Is there a way to specify plugin options from ZEND's primary application.ini file? Maybe something like this?:

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


I use something like this to pass info to my layouts using bootstrap.

This example is for an application that runs on different domains, thus different layouts. (and has a separate version for MSIE). Each domain as a separate 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);
    }

}

The layout Handler:

<?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);

    }
}

The application.ini:

[production]
layoutName = "Some_File_Name"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜