简体   繁体   中英

Get global config in Zend Framework 2

My question was asked before . I also would like to access to my global configs (config/{,*.}{global,local}.php) located in my personal libraries (in the vendor directory). The closest answer that I think I found is here . I created function in my class

public function getServiceConfig()
{
    return array(
        'factories' => array(
            'funcservice' =>  function(\Zend\ServiceManager\ServiceLocatorInterface $sm) {
                $config = $sm->get('config');
            }
        )
    );
}

And it works however I can't figure out how to get anything from the result.

$config = $this->getServiceConfig();
print_r($config);

gives me

Array
(
[factories] => Array
    (
        [funcservice] => Closure Object
            (
                [this] => Tools\Model\StandartFuncs Object
                    (
                        [eventIdentifier:protected] => Zend\Mvc\Controller\AbstractActionController

                        [plugins:protected] => 
                        [request:protected] => 
                        [response:protected] => 
                        [event:protected] => 
                        [events:protected] => 
                        [serviceLocator:protected] => 
                    )

                [parameter] => Array
                    (
                        [$sm] => <required>
                    )

            )

    )
)

and from $config = $this->getServiceConfig()->get('locales'); I get

Fatal error: Call to a member function get() on a non-object

Let's assume you have a locales config file locales.local.php :

<?php

return array(
    'hostname' => 'http://apachehost'
);

These global and local config files should be in the config/autoload folder.

Folder structure:

- root
  - config
    - autoload
      - locales.global.php
      - locales.local.php
    - application.config.php

Then you load them using the following line in your application.config.php . Details on this advanced configuration you can read here in the ZF2 documentation

'module_listener_options' => array(
    'config_glob_paths' => array(
        'config/autoload/{{,*.}global,{,*.}local}.php',
    ),
)

Now you can access your config from your ServiceManager instance like this:

$config = $serviceManager->get('Config');

This $config variable is an array. So you cannot access anything with getters. You are supposed to use array notation:

$locales = $config['locales'];

If your really want to use getters then you have to make your configuration to an object. You can do this using the Zend\\Config\\Config class like this:

$config = new \Zend\Config\Config($config, false);

Now you can access like you wrote in your question:

$config->get('locales');

Update

If you want to load auto config files from a vendor module it is common practice to copy those *.local.php and/or *.global.php files that come with the module to the autoload folder and edit the copied files according to your needs.

I don't think you've quite understood the solution you're trying to implement. The factory you're adding to the service config needs to return an instance of your library class. The reason you're putting it in a factory is so that you can inject the config array into it. So your code should look more like this:

public function getServiceConfig()
{
    return array(
        'factories' => array(
            'funcservice' =>  function(\Zend\ServiceManager\ServiceLocatorInterface $sm) {
                $config = $sm->get('config');

                return new \SomeLibrary($config);
            }
        )
    );
}

(where SomeLibrary is the name of your library class in /vendor ).

You will then need to use the service manager to instantiate your library class whenever you need to access it, eg in a controller:

public function someAction()
{
    $yourLibrary = $this->getServiceLocator()->get('funcservice');
}

This will create an instance of your library class, passing the config array as the constructor to the first parameter. You should never need to call getServiceConfig() yourself, and this wouldn't achieve anything.

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