How can I access the configuration of a Zend Frame

2019-03-08 05:40发布

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?

7条回答
太酷不给撩
2楼-- · 2019-03-08 06:13
$this->getInvokeArg('bootstrap')->getOptions();
// or 

$configDb = $this->getInvokeArg('bootstrap')->getOption('db');
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-03-08 06:18

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();
    }
}
查看更多
三岁会撩人
4楼-- · 2019-03-08 06:29

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;
查看更多
Bombasti
5楼-- · 2019-03-08 06:32

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');
查看更多
smile是对你的礼貌
6楼-- · 2019-03-08 06:35

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

$my = $this->getInvokeArg('bootstrap')->getOption('my');
查看更多
我欲成王,谁敢阻挡
7楼-- · 2019-03-08 06:35

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();
    }
}
查看更多
登录 后发表回答