简体   繁体   English

在Zend Framework 2中获取全局配置

[英]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). 我还想访问位于我的个人库(在供应商目录中)的全局配置(config / {,*。} {global,local} .php)。 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'); 并从$config = $this->getServiceConfig()->get('locales'); I get 我懂了

Fatal error: Call to a member function get() on a non-object 致命错误:在非对象上调用成员函数get()

Let's assume you have a locales config file locales.local.php : 假设您有一个语言环境配置文件locales.local.php

<?php

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

These global and local config files should be in the config/autoload folder. 这些全局和本地配置文件应位于config/autoload文件夹中。

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 . 然后,使用application.config.php的以下行加载它们。 Details on this advanced configuration you can read here in the ZF2 documentation 您可以在ZF2文档中阅读有关此高级配置的详细信息

'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: 现在,您可以像这样从ServiceManager实例访问配置:

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

This $config variable is an array. $config变量是一个数组。 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: 您可以使用Zend\\Config\\Config类执行以下操作:

$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. 如果要从供应商模块加载自动配置文件,通常的做法是将模块随附的*.local.php和/或*.global.php文件复制到autoload文件夹中,然后根据您的需求编辑复制的文件需要。

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 ). (其中SomeLibrary/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. 这将创建您的库类的实例,将config数组作为构造函数传递给第一个参数。 You should never need to call getServiceConfig() yourself, and this wouldn't achieve anything. 您永远不需要自己调用getServiceConfig() ,这不会实现任何目的。

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

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