How to Override any Part of a BundleEdit this page
Warning: You are browsing the documentation for Symfony 3.4, which is no longer maintained.
Read the updated version of this page for Symfony 6.1 (the current stable version).
This document is a quick reference for how to override different parts of third-party bundles without using How to Use Bundle Inheritance to Override Parts of a Bundle, which is deprecated since Symfony 3.4.
The bundle overriding mechanism means that you cannot use physical paths to
refer to bundle's resources (e.g.
use logical paths in your bundles (e.g.
and call the locateResource() method
to turn them into physical paths when needed.
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.
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.
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).
If you want to modify the services created by a bundle, you can use service decoration.
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.
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:
1 2 3 4 5 6 7 8 9 10
# src/Acme/UserBundle/Resources/config/validation.yml FOS\UserBundle\Model\User: properties: plainPassword: - NotBlank: groups: [AcmeValidation] - Length: min: 6 minMessage: fos_user.password.short groups: [AcmeValidation]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
<!-- src/Acme/UserBundle/Resources/config/validation.xml --> <constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> <class name="FOS\UserBundle\Model\User"> <property name="plainPassword"> <constraint name="NotBlank"> <option name="groups"> <value>AcmeValidation</value> </option> </constraint> <constraint name="Length"> <option name="min">6</option> <option name="minMessage">fos_user.password.short</option> <option name="groups"> <value>AcmeValidation</value> </option> </constraint> </property> </class> </constraint-mapping>
Now, update the FOSUserBundle configuration, so it uses your validation groups instead of the original ones.