Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I include an optional config file in Symfony2?

Tags:

symfony

I want to make a local config file, config_local.yml, that allows each development environment to be configured correctly without screwing up other people's dev environments. I want it to be a separate file so that I can "gitignore" it and know that nothing essential is missing from the project, while simultaneously not having the issue of git constantly telling me that config_dev.yml has new changes (and running the risk of someone committing those changes).

Right now, I have config_dev.yml doing

imports:
    - { resource: config_local.yml }

which is great, unless the file doesn't exist (i.e. for a new clone of the repository).

My question is: Is there any way to make this include optional? I.e., If the file exists then import it, otherwise ignore it.

Edit: I was hoping for a syntax like:

imports:
    - { resource: config.yml }
    ? { resource: config_local.yml }
like image 393
Robert Martin Avatar asked Sep 07 '11 18:09

Robert Martin


2 Answers

I know this is a really old question, and I do think the approved solution is better I thought I would give a simpler solution which has the benefit of not changing any code

You can use the ignore_errors option, which won't display any errors if the file doesn't exist

imports:
   - { resource: config_local.yml, ignore_errors: true }

Warning, if you DO have a syntax error in the file, it will also be ignored, so if you have unexpected results, check to make sure there is no syntax error or other error in the file.

like image 133
Kris Avatar answered Nov 01 '22 12:11

Kris


There is another option.

on app/appKernel.php change the registerContainerConfiguration method to this :

 public function registerContainerConfiguration(LoaderInterface $loader)
{
    $loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');

    $extrafiles = array (
        __DIR__.'/config/config_local.yml',
    );

    foreach ($extrafiles as $filename) {
        if (file_exists($filename) && is_readable($filename)) {
             $loader->load($filename);
        }
    }
}

this way you have a global config_local.yml file that overwrites the config_env.yml files

like image 33
Nuno Costa Avatar answered Nov 01 '22 11:11

Nuno Costa