Search code examples
zend-frameworkzend-application

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_Plugin_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"

Solution

  • 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"