How can I access the configuration of a Zend Framework application from a controller?

前端 未结 7 1627
忘掉有多难
忘掉有多难 2021-01-30 14:18

I have a Zend Framework application based on the quick-start setup.

I\'ve gotten the demos working and am now at the point of instantiating a new model class to do some

相关标签:
7条回答
  • 2021-01-30 14:49

    A really simple way to access the configuration options is by directly accessing the globally defined $application variable.

    class My_UserController extends Zend_Controller_Action {
        public function indexAction() {
            global $application;
            $options = $application->getOptions();
        }
    }
    
    0 讨论(0)
  • 2021-01-30 14:51
    $this->getInvokeArg('bootstrap')->getOptions();
    // or 
    
    $configDb = $this->getInvokeArg('bootstrap')->getOption('db');
    
    0 讨论(0)
  • 2021-01-30 14:52

    Alternatively, instead of using Zend_Registry you could also create a singleton Application class that will contain all application info, with public member functions that allow you to access the relevant data. Below you can find a snippet with relevant code (it won't run as is, just to give you an idea how it can be implemented) :

    final class Application
    {
        /**
         * @var Zend_Config
         */    
        private $config = null;
    
        /**
         * @var Application
         */    
        private static $application;
    
        // snip
    
        /**
         * @return Zend_Config
         */
        public function getConfig()
        {
            if (!$this->config instanceof Zend_Config) {
                $this->initConfig();
            }
            return $this->config;
        }
    
        /**
         * @return Application
         */
        public static function getInstance()
        {
            if (self::$application === null) {
                self::$application = new Application();
            }
            return self::$application;
        }
    
        /**
         * Load Configuration
         */
        private function initConfig()
        {
            $configFile = $this->appDir . '/config/application.xml';
            if (!is_readable($configFile)) {
                throw new Application_Exception('Config file "' . $configFile . '" is not readable');
            }
            $config = new Zend_Config_Xml($configFile, 'test');
            $this->config = $config;
        }
    
        // snip
    
        /**
         * @param string $appDir
         */
        public function init($appDir)
        {
            $this->appDir = $appDir;
            $this->initConfig();
            // snip
        }
    
        public function run ($appDir)
        {
            $this->init($appDir);
            $front = $this->initController();
            $front->dispatch();            
        }
    }
    

    Your bootstrap would look like this :

    require 'Application.php';
    try {
        Application::getInstance()->run(dirname(dirname(__FILE__)));
    } catch (Exception $e) {
        header("HTTP/1.x 500 Internal Server Error");
        trigger_error('Application Error : '.$e->getMessage(), E_USER_ERROR);
    }
    

    When you want to access the configuration you would use the following :

    $var = Application::getInstance()->getConfig()->somevar;
    
    0 讨论(0)
  • 2021-01-30 15:02

    I always add the following init-method to my bootstrap to pass the configuration into the registry.

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

    This will shorten your code a little bit:

    class My_UserController extends Zend_Controller_Action
    {
        public function indexAction()
        {
            $manager = new My_Model_Manager(Zend_Registry::get('config')->my);
            $this->view->items = $manager->getItems();
        }
    }
    
    0 讨论(0)
  • 2021-01-30 15:05

    In most ZF apps, the application object is declared in the global scope (see public/index.php in apps created with ZFW_DISTRIBUTION/bin/zf.sh).

    It's not exactly the ZF way, but you can access the object with $GLOBALS['application']. It kinda feels like cheating, but if you're after performance, this will likely be the quickest option.

    $manager = new My_Model_Manager($GLOBALS['application']->getOption('my'));
    
    0 讨论(0)
  • 2021-01-30 15:06

    Since version 1.8 you can use the below code in your Controller:

    $my = $this->getInvokeArg('bootstrap')->getOption('my');
    
    0 讨论(0)
提交回复
热议问题