简体   繁体   English

Symfony 2:是否可以在 YAML 文件中导入 PHP 文件?

[英]Symfony 2 : Is it possible to import a PHP file in YAML file?

I am trying to pass a variable from PHP config file to a YAML file, I am not sure it will work or not as I cannot test it because of some token issues which is yet to be provided by client.我正在尝试将变量从 PHP 配置文件传递到 YAML 文件,我不确定它是否会起作用,因为由于客户端尚未提供的某些令牌问题,我无法对其进行测试。

What I did is:我所做的是:

In PHP file Config.php made an object of class Config:在 PHP 文件 Config.php 中创建了一个类 Config 的对象:

$obj = new Config();
$obj->processEnv();
$obj->define('PROD_PASSWORD', '12345');

In YAML file:在 YAML 文件中:

imports:
    - { resource: Config.php }

prod:
   doctrine:
   class: sfDoctrineDatabase
param:
   username: username
   password: %PROD_PASSWORD%    

Please advise that is it a good practice to do or there are other ways I can achieve this?请告知这是一个很好的做法还是有其他方法可以实现这一目标?

Yes, mixing formats is perfectly fine and in some cases Symfony even recommends different formats, for example when you write a bundle that will be distributed the Best Practices recommend using XML whereas by default in your application YAML is the recommended choice.是的,混合格式非常好,在某些情况下 Symfony 甚至推荐不同的格式,例如,当您编写将分发的包时,最佳实践建议使用 XML,而在您的应用程序中,默认情况下推荐使用 YAML。 There is no performance drawback when using one format over the other (in production) as the configuration will be compiled and cached as a .php file in any case.使用一种格式而不是另一种格式(在生产中)时没有性能缺陷,因为在任何情况下,配置都将编译并缓存为 .php 文件。 As long as an appropriate loader is registered in the kernel it will be able to load the config and merge it with the rest.只要在内核中注册了适当的加载器,它就能够加载配置并将其与其余配置合并。

As a best practice you should stick with one format to make it easier for others joining the project.作为最佳实践,您应该坚持使用一种格式,以便其他人更容易加入项目。 Since all formats support the same features and performance is not affected by the format I don't see a reason to mix styles.由于所有格式都支持相同的功能并且性能不受格式的影响,因此我认为没有理由混合样式。

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

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