Inheritance of bundles

In the Symfony framework, there is an interesting system of bundle inheritance. It is interesting because it works like object-oriented inheritance, but in reverse. When a bundle is inherited, modifications also affect the parent bundle. This is very convenient: if you don't like a certain component or you want to extend/replace its implementation, you can use the mechanism of bundle inheritance without modifying the parent bundle. However, when using the parent bundle, the new functionality will be used.

Let's consider the mechanism of bundle inheritance:

To do this, it is sufficient to create a bundle and override the getParent() method in the main file:

    public function getParent()
    {
        return 'FOSUserBundle';
    }

In this example, we have inherited the FOSUserBundle.

Now, to override any file of the parent, simply create its "variant" in the child bundle, replicating the file structure (path, name).

Most often, the mechanism of bundle inheritance is used to override Twig templates, but there is no limitation to extending the logic of a bundle as well.

In trivial cases, when only 1-2 Twig files need to be overridden, it is not necessary to create a separate bundle. Instead, you can simply replicate the file structure and place the files in the app/resources directory. This approach would be justified, for example, for customizing error pages (404, 500, etc.), but it may not be suitable for specific templates.

Bundle inheritance can be conveniently combined with regular object-oriented inheritance (when overriding controllers, logic, etc.).