Tito Miguel Costa
Refactoring ideas

How to define a bundle as having a parent

Bundle hierarchy in Symfony2

As mentioned in the Symfony2 documentation, bundles can be organized hierarchically, this makes it easy to override controllers or templates. Unfortunately, the documentation lacks in showing how this can be achieved.

Imagine that you are using the FOSUserBundle, and you create your own bundle named ZorbusUserBundle, to make ZorbusUserBundle a child of FOSUserBundle you need to create a method getParent in the ZorbusUserBundle class (src/Zorbus/UserBundle/ZorbusUserBundle.php) and return a string with the name of FOSUserBundle.

<?php
namespace Zorbus\UserBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class ZorbusUserBundle extends Bundle
{
    public function getParent()
    {
        return ‘FOSUserBundle’;
    }
}

Don’t forget to clear the cache to make it work, even in dev environment. For this moment on, just replicate the structure of the FOSUserBundle in ZorbusUserBundle and it will be the one used by Symfony2, overrinding the parent bundle.

22 March, 2012
Tutorial
Symfony2, Doctrine, Cache

Comments