开发者

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

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 real work. In my controller I want to pass a configuration parameter (specified in the application.ini) to my model constructor, something like this:

class My_UserController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $options = $this->getFrontController()->getParam('bootstrap')->getApplication()->getOptions();
        $manager = new My_Model_Manager($options['my']);
        $this->view->items = $manager->getItems();
    }
}

The example above does allow access to the options, but seems extremely round-about. Is开发者_如何学运维 there a better way to access the configuration?


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();
    }
}


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

$my = $this->getInvokeArg('bootstrap')->getOption('my');


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;


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


$this->getInvokeArg('bootstrap')->getOptions();
// or 

$configDb = $this->getInvokeArg('bootstrap')->getOption('db');


I've define a short hand in some place I require_once() in the beginning of boostrap:

function reg($name, $value=null) {
    (null===$value) || Zend_Registry::set($name, $value);
    return Zend_Registry::get($name);
}

and in the bootstrap I have a:

protected function _initFinal()
{
    reg('::app', $this->getApplication());
}

then I can get the Application instance anywhere by use:

$app = reg('::app');


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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜