Development

HowToPlanConfigurationPlacement

You must first sign up to be able to contribute.

Version 2 (modified by dreglad@gmail.com, 11 years ago)
--

Configuration Handling

Preface: It took my awhile to fine tune my placement of configuration settings as I built my first Symfony application. Maybe the notes below will get you to proper config placement quicker than me.

The default configuration handling for symfony allows you to set the configuration of the current request based on both the environment (test, production, dev, etc) as well as the location of request (proj, app, module). When using the default configuration files it works really well to set settings for a single application or a single module, but for many settings you need more flexibility.

In particular, we often need a settings array for multiple apps or modules within the project but we may not want to load the settings for every single request unless they are needed. For this reason, we do some special loading of global settings on an as needed bases.

For example, the listings have checkbox set options (Utilities Included => Water, Sewer, Garbage, etc) that we will need in the frontend app when we edit the listing, in the sites app when we show the listing, and in the backend app when we force editing of a listing by the admin. There are two ways to get these settings across all apps:

  • load the setting for every request in all apps
  • load the setting only when needed from a global settings folder

We have chosen to use the second option so the application runs quicker. So for our listings example, we need the settings in the frontend whenever working with the listings module. So we add and edit:

And we include a request to load global settings like:

/frontend/modules/listings/config/config.php:

sfConfig::add(sfYaml::load(sfConfig::get('sf_config_global_dir').DIRECTORY_SEPARATOR.'listings.yml'));

NOTE: We add sf_config_global_dir setting in /config/config.php with this line:

sfConfig::set('sf_config_global_dir', sfConfig::get('sf_config_dir').DIRECTORY_SEPARATOR.'global');  

This will then load our options into the sfConfig settings for the listings. Then when we need these same options in the sites app we just add the same line to:

  • /sites/modules/listings/config/config.php

and same for backend. This way we get global (project level) config settings but only when we need them.

NOTE: By default this configuration is not cached, see HowToCacheCustomConfiguration

General Rules for Config Placement

If the setting will be likely be used in more than one app:

  • If the setting will be used in most modules or all requests, put in /config/config.php
  • If only in a couple modules per app, put in /config/global/ and load when needed using method above.

If the setting will definitely ONLY be used in ONE app:

  • If used in ONLY ONE module, put in /apps/app_name/modules/config/module.yml
  • If used in several modules in app, put in /apps/app_name/config/app.yml