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

Viewed 438

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.

What I did is:

In PHP file Config.php made an object of class Config:

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

In YAML file:

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?

1 Answers

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. 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. 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.

Related