Skip to content

Commit 8c97795

Browse files
committed
[cookbook][bundles] Adding the bundle parent entry and creating another blank entry to be a reference for overriding pieces of a bundle. This closes #354
1 parent 9d31454 commit 8c97795

File tree

4 files changed

+113
-5
lines changed

4 files changed

+113
-5
lines changed

cookbook/bundles/inheritance.rst

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,94 @@
11
.. index::
22
single: Bundle; Inheritance
3-
single: Bundle; Overriding
43

54
How to use Bundle Inheritance to Override parts of a Bundle
65
===========================================================
76

8-
This article has not been written yet, but will soon. If you're interested
9-
in writing this entry, see :doc:`/contributing/documentation/overview`.
7+
When working with third-party bundles, you'll probably come across a situation
8+
where you want to override a file in that third-party bundle with a file
9+
in one of your own bundles. Symfony gives you a very convenient way to override
10+
things like controllers, templates, translations, and other files in a bundle's
11+
``Resources/`` directory.
1012

11-
This topic is meant to show how you can make one bundle "extend" another
12-
and use this to override different aspects of that bundle.
13+
For example, suppose that you're installing the `FOSUserBundle`_, but you
14+
want to override its base ``layout.html.twig`` template, as well as one of
15+
its controllers. Suppose also that you have your own ``AcmeUserBundle``
16+
where you want the overridden files to live. Start by registering the ``FOSUserBundle``
17+
as the "parent" of your bundle::
18+
19+
// src/Acme/UserBundle/AcmeUserBundle.php
20+
namespace Acme\UserBundle;
21+
22+
use Symfony\Component\HttpKernel\Bundle\Bundle;
23+
24+
class AcmeUserBundle extends Bundle
25+
{
26+
public function getParent()
27+
{
28+
return 'FOSUserBundle';
29+
}
30+
}
31+
32+
By making this simple change, you can now override several parts of the ``FOSUserBundle``
33+
simply by creating a file with the same name.
34+
35+
Overriding Controllers
36+
~~~~~~~~~~~~~~~~~~~~~~
37+
38+
Suppose you want to add some functionality to the ``registerAction`` of a
39+
``RegistrationController`` that lives inside ``FOSUserBundle``. To do so,
40+
just create your own ``RegistrationController.php`` file, override the bundle's
41+
original method, and change its functionality::
42+
43+
// src/Acme/UserBundle/Controller/RegistrationController.php
44+
namespace Acme\UserBundle\Controller;
45+
46+
use FOS\UserBundle\Controller\RegistrationController as BaseController;
47+
48+
class RegistrationController extends BaseController
49+
{
50+
public function registerAction()
51+
{
52+
$response = parent:registerAction();
53+
54+
// do custom stuff
55+
56+
return $response;
57+
}
58+
}
59+
60+
.. tip::
61+
62+
Depending on how severely you need to change the behavior, you might
63+
call ``parent::registerAction()`` or completely replace its logic with
64+
your own.
65+
66+
.. note::
67+
68+
Overriding controllers in this way only works if the bundle refers to
69+
the controller using the standard ``FOSUserBundle:Registration:register``
70+
syntax in routes and templates. This is the best practice.
71+
72+
Overriding Resources: Templates, Routing, Translations, Validation, etc
73+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
74+
75+
Most resources can also be overridden, simply by creating a file in the same
76+
location as your parent bundle.
77+
78+
For example, it's very common to need to override the ``FOSUserBundle``'s
79+
``layout.html.twig`` template so that it uses your application's base layout.
80+
Since the file lives at ``Resources/views/layout.html.twig`` in the ``FOSUserBundle``,
81+
you can create your own file in the same location of ``AcmeUserBundle``.
82+
Symfony will ignore the file that lives inside the ``FOSUserBundle`` entirely,
83+
and use your file instead.
84+
85+
The same goes for routing files, validation configuration and other resources.
86+
87+
.. note::
88+
89+
The overriding of resources only works when you refer to resources with
90+
the ``@FosUserBundle/Resources/config/routing/security.xml`` method.
91+
If you refer to resources without using the @BundleName shortcut, they
92+
can't be overridden in this way.
93+
94+
.. _`FOSUserBundle`: https://github.com/friendsofsymfony/fosuserbundle

cookbook/bundles/override.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
.. index::
2+
single: Bundle; Inheritance
3+
4+
How to Override any Part of a Bundle
5+
====================================
6+
7+
This article has not been written yet, but will soon. If you're interested
8+
in writing this entry, see :doc:`/contributing/documentation/overview`.
9+
10+
This topic is meant to show how you can override each and every part of a
11+
bundle, both from your application and from other bundles. This may include:
12+
13+
* Templates
14+
* Routing
15+
* Controllers
16+
* Services & Configuration
17+
* Entities & Entity mapping
18+
* Forms
19+
* Validation metadata
20+
21+
In some cases, this may talk about the best practices that a bundle must
22+
use in order for certain pieces to be overridable (or easily overridable).
23+
We may also talk about how certain pieces *aren't* really overridable, but
24+
your best approach at solving your problems anyways.

cookbook/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Cookbook
3838

3939
bundles/best_practices
4040
bundles/inheritance
41+
bundles/override
4142
bundles/extension
4243

4344
email

cookbook/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949

5050
* :doc:`/cookbook/bundles/best_practices`
5151
* :doc:`/cookbook/bundles/inheritance`
52+
* :doc:`/cookbook/bundles/override`
5253
* :doc:`/cookbook/bundles/extension`
5354

5455
* **Emailing**

0 commit comments

Comments
 (0)