简体   繁体   English

Zend Framework 2中的全局访问配置

[英]Access config global in Zend Framework 2

I would like to access to my global configs ( config/{,*.}{global,local}.php ) located in my personal libraries (in the vendor directory). 我想访问位于我的个人库(在vendor目录中)的全局配置( config/{,*.}{global,local}.php )。 But despite my search in the web, I did not succeed to achieve this. 但是,尽管我在网络上进行搜索,但还是没有成功实现这一目标。

I know how to access the config file of a module : 我知道如何访问模块的配置文件:

\MyModule\Module::getConfig()

I know how to access the global configurations from a controller : 我知道如何从控制器访问全局配置:

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

But how to access these from a file inside the vendor directory ? 但是如何从供应商目录中的文件访问这些文件? My libraries do not extend anything, it may be the root of the problem ? 我的库没有扩展任何内容,这可能是问题的根源吗? Can I use a static method to access these ? 我可以使用静态方法来访问这些吗?

Thanks. 谢谢。

You can get it through the serviceManager. 您可以通过serviceManager获得它。 In controller: 在控制器中:

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

Otherwise same way, just you need service Manager, for eaxmple in Module.php: 否则,只需使用Module.php中的eaxmple,就需要Service Manager:

  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']){
                ..................

easy way to do this. 简单的方法做到这一点。 not only in module but anywhere. 不仅在模块中,而且在任何地方。

$config = new \Zend\Config\Config( include APPLICATION_PATH.'/config/autoload/global.php' ); 

echo $config->myconfig;

define APPLICATION_PATH in public/index.php 在public / index.php中定义APPLICATION_PATH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM