Category Archives: Symfony2

Expose config.yml values globally for Twig templates in Symfony2 applications

Lets suppose you have created a bundle that has some specific configuration in the config.yml file (this is done by exposing semantic configuration). For example the languages supported by the application:

        en: English
        es: Spanish (Español)

You could make all your controllers read this language configuration from the container interface and then expose that variable to the templates like this:

// src/Acme/DemoBundle/Controller/AcmeController.php
$this->languages = $this->container->getParameter('acme_demo.languages');

    public function indexAction()
        return $this->render('AcmeDemoBundle:Default:index.html.twig',
                array('languages' => $this->languages)
// src/Acme/DemoBundle/Resources/views/Default/index.html.twig
{% for lang, language in languages %}
                    {{ lang }} - {{ language }}
{% endfor %}

But this would be VERY tedious. A much better approach is to create a Twig extension that exposes that configuration readed from the container interface as it would be done inside config.yml in the twig:globals section:

// src/Acme/DemoBundle/Twig/LanguageExposeExtension.php
container = $container;
    public function getGlobals()
        return array(
            'languages' => $this->container->getParameter('acme_demo.languages')
    public function getName()
        return 'language_expose_extension';

And of course register this class as a Twig extension using the twig.extension tag:

// src/Acme/DemoBundle/Resources/config/services.yml
        class: Acme\DemoBundle\Twig\LanguageExposeExtension
        arguments: []
            - { name: twig.extension }

Now you can remove the language parameter in the render call because it’s now defined globally for all templates.

Fix “Cannot redeclare class Symfony\…” in Symfony2 after upgrade

After upgrading from Symfony 2.2 to 2.3 I got this error in my application:

Fatal error: Cannot redeclare class Symfony\Component\HttpFoundation\FileBag in app/cache/prod/classes.php on line 1823

Another symptom was this error while trying to clear the cache for the prod environment:

The parameter "kernel.http_method_override" must be defined.

The process to solve this problem was:

1.- Remove the cache folder:

rm -rf app/cache/prod

2.- Then clear the cache for the prod environment:

php app/console cache:clear --env=prod --no-debug


Update composer and all packages installed with it

php composer.phar self-update
php composer.phar update

Symfony2 – ErrorException: Notice: serialize() [function.serialize]: “xxx” returned as member variable from __sleep() but does not exist in …

While storing an entity from Doctrine2 in the session under a Symfony2 application I got this error:

ErrorException: Notice: serialize() [function.serialize]: "entity_property" returned as member variable from __sleep() but does not exist in ...vendor/symfony/symfony/src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php line 27

This error was happening because my “entity_property” was declared as private and PHP has some issues serializing objects that subclass a class with private properties. Just changing that “entity_property” to protected (as it should always have been…) solver the error.


Install Doctrine2 with PEAR in MacPorts

Run those commands:

sudo /opt/local/bin/pear channel-discover
sudo /opt/local/bin/pear channel-discover
sudo /opt/local/bin/pear install -a
sudo /opt/local/bin/pear install
sudo /opt/local/bin/pear install

Then running doctrine -v should work but /opt/local/lib/php must be in the include_path of your PHP installation. I tried to include it in my /opt/local/etc/php5/php.ini file but didn’t work, so I added this line as the very first line in /opt/local/bin/doctrine.php:

set_include_path(get_include_path() . PATH_SEPARATOR . '/opt/local/lib/php');

Then everything worked fine.


  • Page 1 of 2
  • 1
  • 2
  • >