Tito Miguel Costa
Refactoring ideas

How to set custom parameters in a Symfony2 project

Use the service container to manage config values

In symfony 1.* we had app.yml, so we would define custom parameters and access them anywhere with the get static method of the sfConfig class. In Symfony2, there is no such thing as app.yml. In case we want to define custom parameters, we create a new file in app/config, for instance, settings.yml or settings.ini, an include it in the config.yml as an external resource (we could use the existing file parameters.ini, but not to mess up, I prefer to use a different file).

So, in config.yml (or just in config_dev.yml if you prefer to make it available only in the dev environment)

    - { resource: settings.yml }

In settings.yml define your parameters:

    shared_folder: '/home/shared'

To access this parameter in a controller:

public function indexAction()
    $shared_folder = $this->container->getParameter('shared_folder');
5 May, 2012
Symfony2, Container, Parameters