简体   繁体   中英

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). 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. In controller:

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

Otherwise same way, just you need service Manager, for eaxmple in Module.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']){
                ..................

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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