How to Override any Part of a Bundle

How to Override any Part of a Bundle

When using a third-party bundle, you might want to customize or override some of its features. This document describes ways of overriding the most common features of a bundle.

Tip

The bundle overriding mechanism means that you cannot use physical paths to refer to bundle’s resources (e.g. __DIR__/config/services.xml). Always use logical paths in your bundles (e.g. @FooBundle/Resources/config/services.xml) and call the locateResource() method to turn them into physical paths when needed.

Templates

Third-party bundle templates can be overridden in the <your-project>/templates/bundles/<bundle-name>/ directory. The new templates must use the same name and path (relative to <bundle>/Resources/views/) as the original templates.

For example, to override the Resources/views/Registration/confirmed.html.twig template from the FOSUserBundle, create this template: <your-project>/templates/bundles/FOSUserBundle/Registration/confirmed.html.twig

Caution

If you add a template in a new location, you may need to clear your cache (php bin/console cache:clear), even if you are in debug mode.

Instead of overriding an entire template, you may just want to override one or more blocks. However, since you are overriding the template you want to extend from, you would end up in an infinite loop error. The solution is to use the special ! prefix in the template name to tell Symfony that you want to extend from the original template, not from the overridden one:

  1. {# templates/bundles/FOSUserBundle/Registration/confirmed.html.twig #}
  2. {# the special '!' prefix avoids errors when extending from an overridden template #}
  3. {% extends "@!FOSUser/Registration/confirmed.html.twig" %}
  4. {% block some_block %}
  5. ...
  6. {% endblock %}

Tip

Symfony internals use some bundles too, so you can apply the same technique to override the core Symfony templates. For example, you can customize error pages overriding TwigBundle templates.

Routing

Routing is never automatically imported in Symfony. If you want to include the routes from any bundle, then they must be manually imported from somewhere in your application (e.g. config/routes.yaml).

The easiest way to “override” a bundle’s routing is to never import it at all. Instead of importing a third-party bundle’s routing, copy that routing file into your application, modify it, and import it instead.

Controllers

If the controller is a service, see the next section on how to override it. Otherwise, define a new route + controller with the same path associated to the controller you want to override (and make sure that the new route is loaded before the bundle one).

Services & Configuration

If you want to modify the services created by a bundle, you can use service decoration.

If you want to do more advanced manipulations, like removing services created by other bundles, you must work with service definitions inside a compiler pass.

Entities & Entity Mapping

Overriding entity mapping is only possible if a bundle provides a mapped superclass (such as the User entity in the FOSUserBundle). It’s possible to override attributes and associations in this way. Learn more about this feature and its limitations in the Doctrine documentation.

Forms

Existing form types can be modified defining form type extensions.

Validation Metadata

Symfony loads all validation configuration files from every bundle and combines them into one validation metadata tree. This means you are able to add new constraints to a property, but you cannot override them.

To overcome this, the 3rd party bundle needs to have configuration for validation groups. For instance, the FOSUserBundle has this configuration. To create your own validation, add the constraints to a new validation group:

  • YAML

    1. # config/validator/validation.yaml
    2. FOS\UserBundle\Model\User:
    3. properties:
    4. plainPassword:
    5. - NotBlank:
    6. groups: [AcmeValidation]
    7. - Length:
    8. min: 6
    9. minMessage: fos_user.password.short
    10. groups: [AcmeValidation]
  • XML

    1. <!-- config/validator/validation.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping
    6. https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
    7. <class name="FOS\UserBundle\Model\User">
    8. <property name="plainPassword">
    9. <constraint name="NotBlank">
    10. <option name="groups">
    11. <value>AcmeValidation</value>
    12. </option>
    13. </constraint>
    14. <constraint name="Length">
    15. <option name="min">6</option>
    16. <option name="minMessage">fos_user.password.short</option>
    17. <option name="groups">
    18. <value>AcmeValidation</value>
    19. </option>
    20. </constraint>
    21. </property>
    22. </class>
    23. </constraint-mapping>

Now, update the FOSUserBundle configuration, so it uses your validation groups instead of the original ones.

Translations

Translations are not related to bundles, but to translation domains. For this reason, you can override any bundle translation file from the main translations/ directory, as long as the new file uses the same domain.

For example, to override the translations defined in the Resources/translations/FOSUserBundle.es.yml file of the FOSUserBundle, create a <your-project>/translations/FOSUserBundle.es.yml file.

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