44Best Practices for Reusable Bundles
55===================================
66
7- There are two types of bundles:
8-
9- * Application-specific bundles: only used to build your application;
10- * Reusable bundles: meant to be shared across many projects.
11-
127This article is all about how to structure your **reusable bundles ** so that
13- they're easy to configure and extend. Many of these recommendations do not
14- apply to application bundles because you'll want to keep those as simple
15- as possible. For application bundles, just follow the practices shown throughout
16- the guides.
17-
18- ..seealso ::
19-
20- The best practices for application-specific bundles are discussed in
21- :doc: `/best_practices/introduction `.
8+ they're easy to configure and extend. Reusable bundles are those meant to be
9+ shared privately across many company projects or publicly so any Symfony project
10+ can install them.
2211
2312..index ::
2413 pair: Bundle; Naming conventions
@@ -28,10 +17,10 @@ the guides.
2817Bundle Name
2918-----------
3019
31- A bundle is also a PHP namespace. The namespace must follow the `PSR-0 `_ or
32- ` PSR-4 `_ interoperabilitystandards for PHP namespaces and class names: it starts
33- with a vendor segment, followed by zero or more category segments, and it ends
34- with the namespace short name, which must end with ``Bundle ``.
20+ A bundle is also a PHP namespace. The namespace must follow the `PSR-4 `_
21+ interoperabilitystandard for PHP namespaces and class names: it starts with a
22+ vendor segment, followed by zero or more category segments, and it ends with the
23+ namespace short name, which must end with ``Bundle ``.
3524
3625A namespace becomes a bundle as soon as you add a bundle class to it. The
3726bundle class name must follow these simple rules:
@@ -116,21 +105,21 @@ The following classes and files have specific emplacements (some are mandatory
116105and others are just conventions followed by most developers):
117106
118107=================================================== ========================================
119- Type Directory
108+ Type Directory
120109=================================================== ========================================
121- Commands ``Command/ ``
122- Controllers ``Controller/ ``
123- Service Container Extensions ``DependencyInjection/ ``
110+ Commands ``Command/ ``
111+ Controllers ``Controller/ ``
112+ Service Container Extensions ``DependencyInjection/ ``
124113Doctrine ORM entities (when not using annotations) ``Entity/ ``
125114Doctrine ODM documents (when not using annotations) ``Document/ ``
126- Event Listeners ``EventListener/ ``
127- Configuration ``Resources/config/ ``
128- WebResources (CSS, JS, images) ``Resources/public/ ``
129- Translation files ``Resources/translations/ ``
115+ Event Listeners ``EventListener/ ``
116+ Configuration(routes, services, etc.) ``Resources/config/ ``
117+ WebAssets (CSS, JS, images) ``Resources/public/ ``
118+ Translation files ``Resources/translations/ ``
130119Validation (when not using annotations) ``Resources/config/validation/ ``
131120Serialization (when not using annotations) ``Resources/config/serialization/ ``
132- Templates ``Resources/views/ ``
133- Unit and Functional Tests ``Tests/ ``
121+ Templates ``Resources/views/ ``
122+ Unit and Functional Tests ``Tests/ ``
134123=================================================== ========================================
135124
136125Classes
@@ -177,16 +166,26 @@ the ``Tests/`` directory. Tests should follow the following principles:
177166 A test suite must not contain ``AllTests.php `` scripts, but must rely on the
178167 existence of a ``phpunit.xml.dist `` file.
179168
169+ Installation
170+ ------------
171+
172+ Bundles should set ``"type": "symfony-bundle" `` in their ``composer.json `` file.
173+ With this,:doc: `Symfony Flex </setup/flex >` will be able to automatically
174+ enable your bundle when it's installed.
175+
176+ If your bundle requires any setup (e.g. configuration, new files, changes to
177+ `.gitignore `, etc), then you should create a `Symfony Flex recipe `_.
178+
180179Documentation
181180-------------
182181
183182All classes and functions must come with full PHPDoc.
184183
185- Extensive documentation should also be provided in the ``Resources/doc/ ``
184+ Extensive documentation should also be provided in the ``Resources/doc/ ``
186185directory.
187- The index file (for example ``Resources/doc/index.rst `` or
188- ``Resources/doc/index.md ``) is the only mandatory file and must be the entry
189- point for the documentation. The
186+ The index file (for example ``Resources/doc/index.rst `` or
187+ ``Resources/doc/index.md ``) is the only mandatory file and must be the entry
188+ point for the documentation. The
190189:doc: `reStructuredText (rST) </contributing/documentation/format >` is the format
191190used to render the documentation on symfony.com.
192191
@@ -203,8 +202,19 @@ following standardized instructions in your ``README.md`` file.
203202 Installation
204203 ============
205204
206- Step 1: Download the Bundle
207- ---------------------------
205+ Applications that use Symfony Flex
206+ ----------------------------------
207+
208+ Open a command console, enter your project directory and execute:
209+
210+ ```console
211+ $ composer require <package-name>
212+ ```
213+
214+ Applications that don't use Symfony Flex
215+ ----------------------------------------
216+
217+ ### Step 1: Download the Bundle
208218
209219 Open a command console, enter your project directory and execute the
210220 following command to download the latest stable version of this bundle:
@@ -217,8 +227,7 @@ following standardized instructions in your ``README.md`` file.
217227 in the [installation chapter](https://getcomposer.org/doc/00-intro.md)
218228 of the Composer documentation.
219229
220- Step 2: Enable the Bundle
221- -------------------------
230+ ### Step 2: Enable the Bundle
222231
223232 Then, enable the bundle by adding it to the list of registered bundles
224233 in the `app/AppKernel.php` file of your project:
@@ -249,8 +258,20 @@ following standardized instructions in your ``README.md`` file.
249258 Installation
250259 ============
251260
261+ Applications that use Symfony Flex
262+ ----------------------------------
263+
264+ Open a command console, enter your project directory and execute:
265+
266+ .. code-block:: bash
267+
268+ $ composer require <package-name>
269+
270+ Applications that don't use Symfony Flex
271+ ----------------------------------------
272+
252273 Step 1: Download the Bundle
253- ---------------------------
274+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
254275
255276 Open a command console, enter your project directory and execute the
256277 following command to download the latest stable version of this bundle:
@@ -263,7 +284,7 @@ following standardized instructions in your ``README.md`` file.
263284 in the `installation chapter`_ of the Composer documentation.
264285
265286 Step 2: Enable the Bundle
266- -------------------------
287+ ~~~~~~~~~~~~~~~~~~~~~~~~~
267288
268289 Then, enable the bundle by adding it to the list of registered bundles
269290 in the ``app/AppKernel.php`` file of your project:
@@ -422,9 +443,8 @@ The ``composer.json`` file should include at least the following metadata:
422443 a string (or array of strings) with a `valid license identifier `_, such as ``MIT ``.
423444
424445``autoload ``
425- This information is used by Symfony to load the classes of the bundle. The
426- `PSR-4 `_ autoload standard is recommended for modern bundles, but `PSR-0 `_
427- standard is also supported.
446+ This information is used by Symfony to load the classes of the bundle. It's
447+ recommended to use the `PSR-4 `_ autoload standard.
428448
429449In order to make it easier for developers to find your bundle, register it on
430450`Packagist `_, the official repository for Composer packages.
@@ -450,8 +470,8 @@ Learn more
450470*:doc: `/bundles/extension `
451471*:doc: `/bundles/configuration `
452472
453- .. _`PSR-0` :http://www.php-fig.org/psr/psr-0/
454473.. _`PSR-4` :http://www.php-fig.org/psr/psr-4/
474+ .. _`Symfony Flex recipe` :https://github.com/symfony/recipes
455475.. _`Semantic Versioning Standard` :http://semver.org/
456476.. _`Packagist` :https://packagist.org/
457477.. _`choose any license` :http://choosealicense.com/