Skip to content

How to Use Bundle Inheritance to Override Parts of a Bundle

Warning: You are browsing the documentation for Symfony 3.x, which is no longer maintained.

Read the updated version of this page for Symfony 7.2 (the current stable version).

3.4

Bundle inheritance is deprecated since Symfony 3.4 and will be removed in 4.0, but you can override any part of a bundle without using bundle inheritance.

When working with third-party bundles, you'll probably come across a situation where you want to override a file in that third-party bundle with a file in one of your own bundles. Symfony gives you a very convenient way to override things like controllers, templates, and other files in a bundle's Resources/ directory.

For example, suppose that you have installed FOSUserBundle, but you want to override its base layout.html.twig template, as well as one of its controllers.

First, create a new bundle called UserBundle and enable it in your application. Then, register the third-party FOSUserBundle as the "parent" of your bundle:

1
2
3
4
5
6
7
8
9
10
11
12
// src/UserBundle/UserBundle.php
namespace UserBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class UserBundle extends Bundle
{
    public function getParent()
    {
        return 'FOSUserBundle';
    }
}

By making this small change, you can now override several parts of the FOSUserBundle by creating a file with the same name.

Note

Despite the method name, there is no parent/child relationship between the bundles, it is just a way to extend and override an existing bundle.

Overriding Controllers

Suppose you want to add some functionality to the registerAction() of a RegistrationController that lives inside FOSUserBundle. To do so, just create your own RegistrationController.php file, override the bundle's original method, and change its functionality:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// src/UserBundle/Controller/RegistrationController.php
namespace UserBundle\Controller;

use FOS\UserBundle\Controller\RegistrationController as BaseController;

class RegistrationController extends BaseController
{
    public function registerAction()
    {
        $response = parent::registerAction();

        // ... do custom stuff
        return $response;
    }
}

Tip

Depending on how severely you need to change the behavior, you might call parent::registerAction() or completely replace its logic with your own.

Note

Overriding controllers in this way only works if the bundle refers to the controller using the standard FOSUserBundle:Registration:register syntax in routes and templates. This is the best practice.

Overriding Resources: Templates, Routing, etc

Most resources can also be overridden by creating a file in the same location as your parent bundle.

For example, it's very common to need to override the FOSUserBundle's layout.html.twig template so that it uses your application's base layout. Since the file lives at Resources/views/layout.html.twig in the FOSUserBundle, you can create your own file in the same location of UserBundle. Symfony will ignore the file that lives inside the FOSUserBundle entirely, and use your file instead.

The same goes for routing files and some other resources.

Note

The overriding of resources only works when you refer to resources with the @FOSUserBundle/Resources/config/routing/security.xml method. You need to use the @BundleName shortcut when referring to resources so they can be successfully overridden (except templates, which are overridden in a different way, as explained in How to Override Templates from Third-Party Bundles).

Caution

Translation and validation files do not work in the same way as described above. Read "How to Override any Part of a Bundle" if you want to learn how to override translations and see "How to Override any Part of a Bundle" for tricks to override the validation.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version