Symfony2 custom config parameters

Featured Image © cherrychil@sxc.hu

Sometimes we need to add some static configuration options inside our project that will be accessible from whole project scope.

For example, I wanted to define some custom DateTime format that I will use in more then one bundles to format my output to mobile device.

I didn’t want to write format string again and again in code, because after 2 years as my project expanding, I maybe will not be able to quickly find where I defined that output format to reuse it.

Then I decided to try to put configuration option inside config.yml file and when I change value there, it will be changed elsewhere in the project so I dont need to search and replace this values one by one.

This is my custom parameters:

#config.yml

surgeworks_core:
 api_dateformat: "Y-m-d\TH:i:s\Z"
 api_item_cache_lifetime: 3600
 api_item_cache_enabled: false

Ok. I defined needed parameters inside config file, but I have to find proper way to access them inside application.

Now, let’s modify Configuration.php file a little bit …

//Surgeworks/CoreBundle/DependencyInjection/Configuration.php
 
class Configuration implements ConfigurationInterface
{
/**
* {@inheritDoc}
*/
public function getConfigTreeBuilder()
{
   $treeBuilder = new TreeBuilder();
   $rootNode = $treeBuilder->root('surgeworks_core')->children()
     ->variableNode('api_dateformat')
     ->defaultValue("Y-m-d\TH:i:s\Z")
     ->end()
     ->variableNode('api_item_cache_lifetime')
     ->defaultValue("86400")->end()
     ->booleanNode('api_item_cache_enabled')->defaultTrue()->end();
 
// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.
 
   return $treeBuilder;
  }
}

Of course, we need some more coding inside ‘SurgeworksCoreExtension’:

//Surgeworks/CoreBundle/DependencyInjection/SurgeworksCoreExtension.php
 
class SurgeworksCoreExtension extends Extension
{
/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
    $configuration = new Configuration();
    $config = $this->processConfiguration($configuration, $configs);
 
    $container->setParameter('api_dateformat', $config['api_dateformat']);
    $container->setParameter('api_item_cache_lifetime', $config['api_item_cache_lifetime']);
 
    $container->setParameter('api_item_cache_enabled', $config['api_item_cache_enabled']);
 
    $loader = new Loader\XmlFileLoader
      ($container, new FileLocator(__DIR__.'/../Resources/config'));
    $loader->load('services.xml');
  }
}

I just have to use my new config values inside controller like this:

//...
   $this->container->getParameter('api_dateformat');
//...

This is all for today. Pleasant coding!


9 comments

  1. Can you please explain to defined hierarchy of parameters, e.g.

    users:
    username: user1
    email: user@abc.com
    phone:
    office : xxxxxxxxxxxx
    personal: xxxxxxxxx

  2. I think you’ll have the same result by replacing ‘surgeworks_core’ by ‘parameters’ in your config.yml.

    Without doing all your stuff in Configuration.php etc.

  3. An alternative way would be to just use the parameters.yml (was renamed from parameters.ini lately) in your config directory.

    It would allow you to skip the need for the Configuration.php and SurgeworksCoreExtension.php files and still load it just fine using $this->container->getParameter(‘api_dateformat’) or whatever YAML variable tree you choose…

  4. Hello,

    I really appreciate your Symfony 2 blog posts.

    Is there a way to subscribe to Symfony 2 posts only, excluding all other (Magento) posts?

    Regards,
    Sebastian

  5. You’ve been faster than me… i found this last week and i wanted to write a little article on it. You did it faster, you did it right
    Good Job

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.