在 Zend Framework 2 中访问全局配置


Access config global in Zend Framework 2

我想访问位于我的个人库(vendor目录中)中的全局配置(config/{,*.}{global,local}.php)。但是,尽管我在网上搜索,但我并没有成功实现这一目标。

我知道如何访问模块的配置文件:

'MyModule'Module::getConfig()

我知道如何从控制器访问全局配置:

$this->getServiceLocator()->get('config');

但是如何从供应商目录中的文件访问这些?我的库没有扩展任何东西,这可能是问题的根源?我可以使用静态方法来访问这些吗?

谢谢。

您可以通过服务管理器获取它。在控制器中:

$config = $this->getServiceLocator()->get('Config');

否则,同样的方式,只是您需要服务管理器,用于模块中的 eaxmple.php:

  public function getConfig()
  {
    return include __DIR__ . '/config/module.config.php';
  }
  public function getServiceConfig()
  {
    return array(
        'factories' => array(
            'mail.transport' =>  function($sm) {
                $config = $sm->get('Config');
                 switch($config['mail']['transport']['type']){
                ..................

简单的方法。 不仅在模块中,而且在任何地方。

$config = new 'Zend'Config'Config( include APPLICATION_PATH.'/config/autoload/global.php' ); 
echo $config->myconfig;

在公共/索引中定义APPLICATION_PATH.php