Skip to content

Commit 8254761

Browse files
committed
Reviewed the Bundles cookbook articles
1 parent 9fb296d commit 8254761

File tree

5 files changed

+71
-59
lines changed

5 files changed

+71
-59
lines changed

Diff for: cookbook/bundles/best_practices.rst

+14-29
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Best Practices for Reusable Bundles
55
===================================
66

7-
There are 2 types of bundles:
7+
There are two types of bundles:
88

99
* Application-specific bundles: only used to build your application;
1010
* Reusable bundles: meant to be shared across many projects.
@@ -13,12 +13,8 @@ This article is all about how to structure your **reusable bundles** so that
1313
they're easy to configure and extend. Many of these recommendations do not
1414
apply to application bundles because you'll want to keep those as simple
1515
as possible. For application bundles, just follow the practices shown throughout
16-
the book and cookbook.
17-
18-
.. seealso::
19-
20-
The best practices for application-specific bundles are discussed in
21-
:doc:`/best_practices/introduction`.
16+
the :doc:`book </book/index>`, the :doc:`cookbook </cookbook/index>` and the
17+
:doc:`best practices </best_practices/index>` book.
2218

2319
.. index::
2420
pair: Bundle; Naming conventions
@@ -38,7 +34,7 @@ bundle class name must follow these simple rules:
3834

3935
* Use only alphanumeric characters and underscores;
4036
* Use a CamelCased name;
41-
* Use a descriptive and short name (no more than 2 words);
37+
* Use a descriptive and short name (no more than two words);
4238
* Prefix the name with the concatenation of the vendor (and optionally the
4339
category namespaces);
4440
* Suffix the name with ``Bundle``.
@@ -112,7 +108,7 @@ The following files are mandatory:
112108
structure to work.
113109

114110
The depth of sub-directories should be kept to the minimal for most used
115-
classes and files (2 levels at a maximum). More levels can be defined for
111+
classes and files (two levels at a maximum). More levels can be defined for
116112
non-strategic, less-used files.
117113

118114
The bundle directory is read-only. If you need to write temporary files, store
@@ -158,7 +154,7 @@ instance, a ``HelloController`` controller is stored in
158154
``Bundle/HelloBundle/Controller/HelloController.php`` and the fully qualified
159155
class name is ``Bundle\HelloBundle\Controller\HelloController``.
160156

161-
All classes and files must follow the Symfony coding :doc:`standards </contributing/code/standards>`.
157+
All classes and files must follow the :doc:`Symfony coding standards </contributing/code/standards>`.
162158

163159
Some classes should be seen as facades and should be as short as possible, like
164160
Commands, Helpers, Listeners, and Controllers.
@@ -181,7 +177,7 @@ Tests
181177
-----
182178

183179
A bundle should come with a test suite written with PHPUnit and stored under
184-
the ``Tests/`` directory. Tests should follow the following principles:
180+
the ``Tests/`` directory. Tests should follow these principles:
185181

186182
* The test suite must be executable with a simple ``phpunit`` command run from
187183
a sample application;
@@ -190,13 +186,14 @@ the ``Tests/`` directory. Tests should follow the following principles:
190186
* The tests should cover at least 95% of the code base.
191187

192188
.. note::
189+
193190
A test suite must not contain ``AllTests.php`` scripts, but must rely on the
194191
existence of a ``phpunit.xml.dist`` file.
195192

196193
Documentation
197194
-------------
198195

199-
All classes and functions must come with full PHPDoc.
196+
All classes and functions must be fully documented using `phpDocumentor`_ tags.
200197

201198
Extensive documentation should also be provided in the
202199
:doc:`reStructuredText </contributing/documentation/format>` format, under
@@ -306,9 +303,6 @@ following standardized instructions in your ``README.md`` file.
306303
307304
.. _`installation chapter`: https://getcomposer.org/doc/00-intro.md
308305
309-
This template assumes that your bundle is in its ``1.x`` version. If not, change
310-
the ``"~1"`` installation version accordingly (``"~2"``, ``"~3"``, etc.)
311-
312306
Optionally, you can add more installation steps (*Step 3*, *Step 4*, etc.) to
313307
explain other required installation tasks, such as registering routes or
314308
dumping assets.
@@ -330,7 +324,8 @@ Translation Files
330324
-----------------
331325

332326
If a bundle provides message translations, they must be defined in the XLIFF
333-
format; the domain should be named after the bundle name (``bundle.hello``).
327+
format; the :ref:`translation domain <using-message-domains>` should be named
328+
after the bundle name (``bundle.hello``).
334329

335330
A bundle must not override existing messages from another bundle.
336331

@@ -369,27 +364,17 @@ The end user can provide values in any configuration file:
369364
// app/config/config.php
370365
$container->setParameter('acme_hello.email.from', 'fabien@example.com');
371366
372-
.. code-block:: ini
373-
374-
; app/config/config.ini
375-
[parameters]
376-
acme_hello.email.from = fabien@example.com
377-
378367
Retrieve the configuration parameters in your code from the container::
379368

380369
$container->getParameter('acme_hello.email.from');
381370

382371
Even if this mechanism is simple enough, you are highly encouraged to use the
383-
semantic configuration described in the cookbook.
372+
:doc:`semantic bundle configuration </cookbook/bundles/extension>` instead.
384373

385374
.. note::
386375

387376
If you are defining services, they should also be prefixed with the bundle
388377
alias.
389378

390-
Learn more from the Cookbook
391-
----------------------------
392-
393-
* :doc:`/cookbook/bundles/extension`
394-
395-
.. _standards: http://www.php-fig.org/psr/psr-4/
379+
.. _`standards`: http://www.php-fig.org/psr/psr-0/
380+
.. _`phpDocumentor`: http://www.phpdoc.org/

Diff for: cookbook/bundles/configuration.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ How to Create Friendly Configuration for a Bundle
66
=================================================
77

88
If you open your application configuration file (usually ``app/config/config.yml``),
9-
you'll see a number of different configuration "namespaces", such as ``framework``,
9+
you'll see a number of different configuration sections, such as ``framework``,
1010
``twig`` and ``doctrine``. Each of these configures a specific bundle, allowing
11-
you to configure things at a high level and then let the bundle make all the
11+
you to define options at a high level and then let the bundle make all the
1212
low-level, complex changes based on your settings.
1313

14-
For example, the following tells the FrameworkBundle to enable the form
15-
integration, which involves the definition of quite a few services as well
14+
For example, the following configuration tells the FrameworkBundle to enable the
15+
form integration, which involves the definition of quite a few services as well
1616
as integration of other related components:
1717

1818
.. configuration-block::

Diff for: cookbook/bundles/extension.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ How to Load Service Configuration inside a Bundle
66
=================================================
77

88
In Symfony, you'll find yourself using many services. These services can be
9-
registered in the `app/config` directory of your application. But when you
9+
registered in the ``app/config/`` directory of your application. But when you
1010
want to decouple the bundle for use in other projects, you want to include the
1111
service configuration in the bundle itself. This article will teach you how to
1212
do that.
@@ -15,7 +15,7 @@ Creating an Extension Class
1515
---------------------------
1616

1717
In order to load service configuration, you have to create a Dependency
18-
Injection Extension for your bundle. This class has some conventions in order
18+
Injection (DI) Extension for your bundle. This class has some conventions in order
1919
to be detected automatically. But you'll later see how you can change it to
2020
your own preferences. By default, the Extension has to comply with the
2121
following conventions:

Diff for: cookbook/bundles/inheritance.rst

+12-12
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ things like controllers, templates, and other files in a bundle's
1212

1313
For example, suppose that you're installing the `FOSUserBundle`_, but you
1414
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::
15+
its controllers. Suppose also that you have your own UserBundle where you want
16+
the overridden files to live. Start by registering the FOSUserBundle as the
17+
"parent" of your bundle::
1818

19-
// src/Acme/UserBundle/AcmeUserBundle.php
20-
namespace Acme\UserBundle;
19+
// src/UserBundle/UserBundle.php
20+
namespace UserBundle;
2121

2222
use Symfony\Component\HttpKernel\Bundle\Bundle;
2323

24-
class AcmeUserBundle extends Bundle
24+
class UserBundle extends Bundle
2525
{
2626
public function getParent()
2727
{
@@ -45,8 +45,8 @@ Suppose you want to add some functionality to the ``registerAction`` of a
4545
just create your own ``RegistrationController.php`` file, override the bundle's
4646
original method, and change its functionality::
4747

48-
// src/Acme/UserBundle/Controller/RegistrationController.php
49-
namespace Acme\UserBundle\Controller;
48+
// src/UserBundle/Controller/RegistrationController.php
49+
namespace UserBundle\Controller;
5050

5151
use FOS\UserBundle\Controller\RegistrationController as BaseController;
5252

@@ -82,17 +82,17 @@ location as your parent bundle.
8282
For example, it's very common to need to override the FOSUserBundle's
8383
``layout.html.twig`` template so that it uses your application's base layout.
8484
Since the file lives at ``Resources/views/layout.html.twig`` in the FOSUserBundle,
85-
you can create your own file in the same location of AcmeUserBundle.
86-
Symfony will ignore the file that lives inside the FOSUserBundle entirely,
87-
and use your file instead.
85+
you can create your own file in the same location of UserBundle. Symfony will
86+
ignore the file that lives inside the FOSUserBundle entirely, and use your file
87+
instead.
8888

8989
The same goes for routing files and some other resources.
9090

9191
.. note::
9292

9393
The overriding of resources only works when you refer to resources with
9494
the ``@FOSUserBundle/Resources/config/routing/security.xml`` method.
95-
If you refer to resources without using the @BundleName shortcut, they
95+
If you refer to resources without using the ``@BundleName`` shortcut, they
9696
can't be overridden in this way.
9797

9898
.. caution::

Diff for: cookbook/bundles/installation.rst

+39-12
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ A) Add Composer Dependencies
1515
----------------------------
1616

1717
Dependencies are managed with Composer, so if Composer is new to you, learn
18-
some basics in `their documentation`_. This has 2 steps:
18+
some basics in `their documentation`_. This involves two steps:
1919

2020
1) Find out the Name of the Bundle on Packagist
2121
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2222

2323
The README for a bundle (e.g. `FOSUserBundle`_) usually tells you its name
2424
(e.g. ``friendsofsymfony/user-bundle``). If it doesn't, you can search for
25-
the library on the `Packagist.org`_ site.
25+
the bundle on the `Packagist.org`_ site.
2626

27-
.. note::
27+
.. tip::
2828

2929
Looking for bundles? Try searching at `KnpBundles.com`_: the unofficial
3030
archive of Symfony Bundles.
@@ -39,9 +39,12 @@ Now that you know the package name, you can install it via Composer:
3939
$ composer require friendsofsymfony/user-bundle
4040
4141
This will choose the best version for your project, add it to ``composer.json``
42-
and download the library into the ``vendor/`` directory. If you need a specific
43-
version, add a ``:`` and the version right after the library name (see
44-
`composer require`_).
42+
and download its code into the ``vendor/`` directory. If you need a specific
43+
version, include it as the second argument of the `composer require`_ command:
44+
45+
.. code-block:: bash
46+
47+
$ composer require friendsofsymfony/user-bundle "~2.0@dev"
4548
4649
B) Enable the Bundle
4750
--------------------
@@ -68,22 +71,46 @@ The only thing you need to do now is register the bundle in ``AppKernel``::
6871
}
6972
}
7073

74+
By default, Symfony bundles are registered in all the application
75+
:doc:`execution environments <cookbook/configuration/environments>`. If the bundle
76+
is meant to be used only in the development or test environments, register it in
77+
the section below::
78+
79+
// app/AppKernel.php
80+
81+
// ...
82+
class AppKernel extends Kernel
83+
{
84+
// ...
85+
86+
public function registerBundles()
87+
{
88+
$bundles = array(
89+
// ...,
90+
);
91+
92+
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
93+
$bundles[] = new FOS\UserBundle\FOSUserBundle();
94+
}
95+
96+
// ...
97+
}
98+
}
99+
71100
C) Configure the Bundle
72101
-----------------------
73102

74103
It's pretty common for a bundle to need some additional setup or configuration
75104
in ``app/config/config.yml``. The bundle's documentation will tell you about
76-
the configuration, but you can also get a reference of the bundle's config
77-
via the ``config:dump-reference`` command.
78-
79-
For instance, in order to look the reference of the ``assetic`` config you
80-
can use this:
105+
the configuration, but you can also get a reference of the bundle's configuration
106+
via the ``config:dump-reference`` command:
81107

82108
.. code-block:: bash
83109
84110
$ app/console config:dump-reference AsseticBundle
85111
86-
or this:
112+
Instead of the full bundle name, you can also pass the short name used as the root
113+
of the bundle's configuration:
87114

88115
.. code-block:: bash
89116

0 commit comments

Comments
 (0)