Symfony2 custom config parameters

Symfony2 custom config parameters ©

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:


 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 …

class Configuration implements ConfigurationInterface
* {@inheritDoc}
public function getConfigTreeBuilder()
   $treeBuilder = new TreeBuilder();
   $rootNode = $treeBuilder->root('surgeworks_core')->children()
// 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’:

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'));

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


This is all for today. Pleasant coding!

You made it all the way down here so you must have enjoyed this post! You may also like:

Symfony, HWIOAuthBundle and Google Sign-in Branko Ajzele
Branko Ajzele, | 14

Symfony, HWIOAuthBundle and Google Sign-in

Overriding Magento blocks, models, helpers and controllers Damir Korpar
Damir Korpar, | 41

Overriding Magento blocks, models, helpers and controllers

Symfony2 –  remember me cookies Darko Goles
Darko Goles, | 2

Symfony2 – remember me cookies


  1. 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.

  2. 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…

  3. 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?


  4. 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>.

Tell us about your project

Drop us a line. We'd love to know more about your project.