简体   繁体   English

如何在Zend Framework 2上以本地配置访问

[英]How to access at the local configuration on Zend Framework 2

I have some configuration on local/config/xxx/config.ini and a local/config/xxx/config_base.ini . 我在local/config/xxx/config.inilocal/config/xxx/config_base.ini

How can I access at there configuration on Zend Framework 2. I have an instance of the ServiceProvider but I cannot access at these config. 如何在Zend Framework 2上在那里进行配置。我有ServiceProvider的实例,但是无法在这些配置下进行访问。 It only ready the module.config.php (ex with $config = $sm->get('Config'); ) 它只准备好module.config.php (例如$config = $sm->get('Config');

I found that I can use this for read an ini file: 我发现我可以用它来读取一个ini文件:

$config = (new Zend\Config\ReaderIni())->fromFile(getcwd() . '/local/config/xxx/config.ini');

But then how to merge the two configurations? 但是,然后如何合并这两种配置?

Why would do you use .ini extension for your config files? 为什么要对配置文件使用.ini扩展名? As you can read in the documentation on Advanced Configuration Tricks config files should be stored as .php files. 如您所见, 有关高级配置技巧文档,配置文件应存储为.php文件。 They contain arrays holding the keys and values of your configuration and they will be automatically read from the autoload folders and merged with the module configuration files. 它们包含保存您的配置键和值的数组,它们将从自动加载文件夹中自动读取并与模块配置文件合并。

Once configuration is aggregated from all modules, the ConfigListener will also merge application configuration globbed in specified directories (typically config/autoload/). 一旦从所有模块中聚合了配置,ConfigListener还将合并指定目录(通常是config / autoload /)中的应用程序配置。

Here you can also find the folder pattern used to find the files it needs to merge: 在这里,您还可以找到用于查找需要合并的文件的文件夹模式:

 // An array of paths from which to glob configuration files after // modules are loaded. These effectively overide configuration // provided by modules themselves. Paths may use GLOB_BRACE notation. 'config_glob_paths' => array( 'config/autoload/{{,*.}global,{,*.}local}.php', ), 

The advantage is that you don't need a separate file reader and you can access your custom config from the service manager directly like you would do for other configs. 优点是您不需要单独的文件读取器,并且可以像处理其他配置一样直接从服务管理器访问自定义配置。

$config = $sm->get('Config');
$webhost = $config['my_param'];

Another advantage is that you can setup your production environment to cache the config files which will dramatically increase the performance of the bootstrapping procedure (reading files is a slow process). 另一个优点是,您可以将生产环境设置为缓存配置文件,这将大大提高引导过程的性能(读取文件是一个缓慢的过程)。

I would suggest you rename your file extension to .php and follow the documentation for setting up your custom configuration values. 我建议您将文件扩展名重命名为.php并按照文档设置自定义配置值。

When using the skeleton application, the system configuration is by default in config/application.config.php . 使用框架应用程序时,系统配置默认为config/application.config.php

/ config.php
return array(
    'webhost'  => 'www.ekio.rw',
    'database' => array(
        'adapter' => 'pdo_mysql',
        'params'  => array(
            'host'     => 'db.ekio.rw',
            'username' => 'dbuser',
            'password' => 'secret',
            'dbname'   => 'mydatabase'
        )
    )
);

Then, 然后,

// Consumes the configuration array
$config = new Zend\Config\Config(include 'config.php');

// Display a configuration datum (results in 'www.ekio.rw')
echo $config->webhost;

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

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