Loading extra YAML config files in Symfony 1.1

At Dogster, we had a bunch of strings and settings that we wanted to keep out of our actions and place into their own YAML files instead of overloading the app.yml file. Here’s how I did it:

You can use this Filter to load the files we’ll setup in app.yml. Place this new PHP file at “symfony_project/apps/frontend/lib/addYmlFilter.class.php”.

class addYmlFilter extends sfFilter<br />
{
  public function execute($filterChain)
  {
    // Execute this filter only once
    if ($this-&gt;isFirstCall() &amp;&amp; is_array(sfConfig::get(&#8216;app_load_yml_files&#8217;)))
    {
      // Filters don&#8217;t have direct access to the request and user objects.
      // You will need to use the context object to get them
      foreach(sfConfig::get(&#8216;app_load_yml_files&#8217;) as $file){
        @include(sfContext::getInstance()-&gt;getConfigCache()-&gt;checkConfig(&#8216;config/&#8217;.$file.&#8217;.yml&#8217;));
      }
    }
    // Execute next filter
    $filterChain-&gt;execute();
  }<br />
}

That filter takes care of all your caching and per-environment settings, the same as app.yml or the others in the config folder.

We will now setup four (4) new YAML files (config/paypal.yml, config/email.yml, config/flash.yml, config/forms.yml)

Place this in your app.yml file:

load:
    yml_files:
        0: paypal
        1: email
        2: flash
        3: forms

Lastly, create a new YAML file “config/config_handlers.yml” with entries for each YAML file we would like to load:

config/paypal.yml:
  class:    sfDefineEnvironmentConfigHandler
  param:
    prefix: paypal_<br />
config/email.yml:
  class:    sfDefineEnvironmentConfigHandler
  param:
    prefix: email_<br />
config/forms.yml:
  class:    sfDefineEnvironmentConfigHandler
  param:
    prefix: forms_<br />
config/flash.yml:
  class:    sfDefineEnvironmentConfigHandler
  param:
    prefix: flash_

Here is some example content you might find within email.yml:

all:
  admin: &#39;admin@example.com&#39;

Once you load up Symfony, you should see all of your new config settings in the debug toolbar under “vars & config” > “SETTINGS”.

If you would like to place the new admin email setting within your code, you can now use sfConfig:

echo sfConfig::get(&#8216;email_admin&#8217;);<br />
//outputs: admin@example.com

Simple!