如何从控制器访问Zend Framework应用程序的配置?

Ada*_*nco 24 php zend-framework

我有一个基于快速启动设置的Zend Framework应用程序.

我已经让演示工作了,现在我正在实例化一个新的模型类来做一些真正的工作.在我的控制器中,我想将配置参数(在application.ini中指定)传递给我的模型构造函数,如下所示:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的例子允许访问选项,但似乎非常圆.有更好的方法来访问配置吗?

Ste*_*rig 48

我总是将以下init方法添加到我的引导程序中,以将配置传递到注册表中.

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

这会稍微缩短您的代码:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 当然,单例总是一些全局变量的OOP方式.然而,注册表模式是在整个应用程序中分发资源的常见且公认的方式.Zend_Registry的使用允许您提供自定义对象作为其存储库,以将其功能扩展到服务定位器或依赖注入容器.GLOBALS方法无法实现所有这些目标.我不明白为什么Zend_Registry方法不应该100%的时间工作. (3认同)
  • 这是不超过$ GLOBALS ["应用"]下面的想法不同,与额外的好处$ GLOBALS ["应用"]工作的时间大概99%. (2认同)

yar*_*son 22

从版本1.8开始,您可以在Controller中使用以下代码:

$my = $this->getInvokeArg('bootstrap')->getOption('my');
Run Code Online (Sandbox Code Playgroud)


wim*_*vds 6

或者,您也可以创建一个包含所有应用程序信息的单例Application类,而不是使用Zend_Registry,其中包含允许您访问相关数据的公共成员函数.您可以在下面找到包含相关代码的代码段(它不会按原样运行,只是为了让您了解它是如何实现的):

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();            
    }
}
Run Code Online (Sandbox Code Playgroud)

你的bootstrap看起来像这样:

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);
}
Run Code Online (Sandbox Code Playgroud)

如果要访问配置,可以使用以下命令:

$var = Application::getInstance()->getConfig()->somevar;
Run Code Online (Sandbox Code Playgroud)