Symfony 2 and php.yml configuration

603 Views Asked by At

I was looking for the best place to set up common PHP options in Symfony project and saw a couple of time that php.yml is being mentioned - but I found that mostly in v1.x documentation, while in v2.x there is no mention of it (or at least I couldn't find it - Symfony search being down for some time doesn't help neither).

The php.yml had nice 'set', 'check' and 'warn' areas where you could fine tune your configuration.

I tried putting the php.yml and setting it up with some options in app/config but it wouldn't work.

Is php.yml abandoned in Symfony 2 and if it is - what would be the next best way to nicely set up PHP options?

As requested, here is a simple sample of php.yml I tried, it's very basic:

## YAML Template.

set:
    display_errors:     on
    error_log:          phperror.log

check:

warn:

Edit: Additional explanation of why I abandoned solution like Matteo suggested

When I did it, something strange was going on. I couldn't pinpoint exactly why, so I left it (I try to avoid all uncertainty when it comes to Symfony because life is so short). So, I made "my_own_config.php" in which I have only a simple line:

ini_set("error_log", "c:/wamp/logs/phperror2.log");

In my config_dev.yml, I include it with - { resource: my_own_config.php }

In my testing controller, I would print

echo '<br>'.ini_get('error_log');

to make sure my settings are really applied & remembered. INITIALLY, when I load the test page for the first time, it would really remember and show the correct new setting, but when I hit F5 after it - it again shows the default, system log. I am running in dev mode all the time. Tried the F5, hard refresh etc, but all of the later calls dropped back down to the default value.

When I'd change my_own_config.php file and resave it - again, the first time after that, my custom setting would be remembered, and on following refreshes it goes away. So, I concluded that some weird Symfony caching takes over and overwrites my custom php config file and abandoned that approach.

2

There are 2 best solutions below

3
On

If you want to do some external framework setting change, you can organize your settings as described in the Miscellaneous Configuration, as example, you can put your custom settings in a files like:

# app/config/my_settings.php

<?php 
error_reporting(E_ALL); 
ini_set("display_errors", 1); 

and include it in your config.yml:

# app/config/config.yml
imports:
    - { resource: my_settings.php }

hope this help

3
On

php.yml was a Symfony1 config file, it is not suitable for use in Symfony2.

As per the Symfony Best Practices page, the app/config/config.yml is suitable for application related settings whilst app/config/parameters.yml.dist (from which parameters.yml is auto-generated) is suitable for infrastructure related settings.

The standard Symfony2 config files found in app/config are:

  • config.yml
  • config_dev.yml
  • config_prod.yml
  • config_test.yml
  • parameters.yml (generated from parameters.yml.dist)
  • parameters.yml.dist
  • routing.yml
  • routing_dev.yml
  • security.yml
  • services.yml

For raw PHP config settings the best place would be the web/app_dev.php (for development) or web/app.php (for production), or system-wide via php.ini. However Symfony has its own PHP error handling service. But you can still turn PHP errors on at the top of the app_dev.php file put - error_reporting(E_ALL); ini_set('display_errors', 1);