简体   繁体   English

在哪里放置我需要的Symfony2自定义参数?

[英]Where to put Symfony2 custom parameters I need?

I need to have some configuration for my bundle in Symfony2 Where is the best place to put them? 我需要在Symfony2中为我的软件包配置一些配置在哪里放置它们的最佳位置?
and how can I retrieve them from there? 以及如何从那里检索它们?

I used my Default DB parameters in PARAMETERS.INI But I need Extra ones that I can retrieve them personally in code. 我在PARAMETERS.INI使用了我的默认数据库参数但是我需要额外的PARAMETERS.INI ,我可以在代码中亲自检索它们。

应该将他的参数保存在bundle中,例如src/Company/SomeBundle/Resources/config/parameters.yml

To define any extra parameters you need, define them in your config.yml file. 要定义所需的任何额外参数,请在config.yml文件中定义它们。 Something like: 就像是:

# app/config/config.yml
parameters:
    my_mailer.class:      Acme\HelloBundle\Mailer
    my_mailer.transport:  sendmail

Then you can retrieve them anywhere that the service container is available, for example inside a controller, just like you retrieve any other service like doctrine or swiftmailer. 然后,您可以在服务容器可用的任何位置检索它们,例如在控制器内部,就像检索任何其他服务(如doctrine或swiftmailer)一样。 For example, in a controller, do 例如,在控制器中,执行

$transport = $this->get('my_mailer.transport');

If you want, you can define these parameters in paramters.ini, you will get the same result. 如果需要,可以在paramters.ini中定义这些参数,您将得到相同的结果。

See the How to expose a Semantic Configuration for a Bundle cookbook entry. 请参阅如何为Bundle cookbook条目公开语义配置 One of the advantages of this approach is that you can validate the configuration. 此方法的一个优点是您可以验证配置。

use different parts in parameters.ini file. 在parameters.ini文件中使用不同的部分。 You could have a prod1 environment using parameters prefixed with prod1 and a prod2 with the same: 你可以有一个prod1环境,使用前缀为prod1的参数和一个带有相同的prod2:

parameters.ini: parameters.ini:

[parameters]
prod1_database_driver = pdo_mysql
prod1_database_host = 127.0.0.1

# ...

prod2_database_driver = pdo_mysql
prod2_database_host = localhost

They both use the prod.yml configuration but overwrite the stuff you want to read from the parameters.ini: 他们都使用prod.yml配置,但覆盖了你想从parameters.ini读取的东西:

config_prod1.yml: config_prod1.yml:

imports:
 - { resource: config_prod.yml }

// .. overwrite stuff here

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

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