4
4
Best Practices for Reusable Bundles
5
5
===================================
6
6
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
-
12
7
This 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.
22
11
23
12
.. index ::
24
13
pair: Bundle; Naming conventions
@@ -28,10 +17,10 @@ the guides.
28
17
Bundle Name
29
18
-----------
30
19
31
- A bundle is also a PHP namespace. The namespace must follow the `PSR-0 `_ or
32
- ` PSR-4 `_ interoperability standards 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
+ interoperability standard 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 ``.
35
24
36
25
A namespace becomes a bundle as soon as you add a bundle class to it. The
37
26
bundle class name must follow these simple rules:
@@ -116,21 +105,21 @@ The following classes and files have specific emplacements (some are mandatory
116
105
and others are just conventions followed by most developers):
117
106
118
107
=================================================== ========================================
119
- Type Directory
108
+ Type Directory
120
109
=================================================== ========================================
121
- Commands ``Command/ ``
122
- Controllers ``Controller/ ``
123
- Service Container Extensions ``DependencyInjection/ ``
110
+ Commands ``Command/ ``
111
+ Controllers ``Controller/ ``
112
+ Service Container Extensions ``DependencyInjection/ ``
124
113
Doctrine ORM entities (when not using annotations) ``Entity/ ``
125
114
Doctrine ODM documents (when not using annotations) ``Document/ ``
126
- Event Listeners ``EventListener/ ``
127
- Configuration ``Resources/config/ ``
128
- Web Resources (CSS, JS, images) ``Resources/public/ ``
129
- Translation files ``Resources/translations/ ``
115
+ Event Listeners ``EventListener/ ``
116
+ Configuration (routes, services, etc.) ``Resources/config/ ``
117
+ Web Assets (CSS, JS, images) ``Resources/public/ ``
118
+ Translation files ``Resources/translations/ ``
130
119
Validation (when not using annotations) ``Resources/config/validation/ ``
131
120
Serialization (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/ ``
134
123
=================================================== ========================================
135
124
136
125
Classes
@@ -177,16 +166,26 @@ the ``Tests/`` directory. Tests should follow the following principles:
177
166
A test suite must not contain ``AllTests.php `` scripts, but must rely on the
178
167
existence of a ``phpunit.xml.dist `` file.
179
168
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
+
180
179
Documentation
181
180
-------------
182
181
183
182
All classes and functions must come with full PHPDoc.
184
183
185
- Extensive documentation should also be provided in the ``Resources/doc/ ``
184
+ Extensive documentation should also be provided in the ``Resources/doc/ ``
186
185
directory.
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
190
189
:doc: `reStructuredText (rST) </contributing/documentation/format >` is the format
191
190
used to render the documentation on symfony.com.
192
191
@@ -203,8 +202,19 @@ following standardized instructions in your ``README.md`` file.
203
202
Installation
204
203
============
205
204
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
208
218
209
219
Open a command console, enter your project directory and execute the
210
220
following command to download the latest stable version of this bundle:
@@ -217,8 +227,7 @@ following standardized instructions in your ``README.md`` file.
217
227
in the [installation chapter](https://getcomposer.org/doc/00-intro.md)
218
228
of the Composer documentation.
219
229
220
- Step 2: Enable the Bundle
221
- -------------------------
230
+ ### Step 2: Enable the Bundle
222
231
223
232
Then, enable the bundle by adding it to the list of registered bundles
224
233
in the `app/AppKernel.php` file of your project:
@@ -249,8 +258,20 @@ following standardized instructions in your ``README.md`` file.
249
258
Installation
250
259
============
251
260
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
+
252
273
Step 1: Download the Bundle
253
- ---------------------------
274
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
254
275
255
276
Open a command console, enter your project directory and execute the
256
277
following command to download the latest stable version of this bundle:
@@ -263,7 +284,7 @@ following standardized instructions in your ``README.md`` file.
263
284
in the `installation chapter`_ of the Composer documentation.
264
285
265
286
Step 2: Enable the Bundle
266
- -------------------------
287
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
267
288
268
289
Then, enable the bundle by adding it to the list of registered bundles
269
290
in the ``app/AppKernel.php`` file of your project:
@@ -422,9 +443,8 @@ The ``composer.json`` file should include at least the following metadata:
422
443
a string (or array of strings) with a `valid license identifier `_, such as ``MIT ``.
423
444
424
445
``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.
428
448
429
449
In order to make it easier for developers to find your bundle, register it on
430
450
`Packagist `_, the official repository for Composer packages.
@@ -450,8 +470,8 @@ Learn more
450
470
* :doc: `/bundles/extension `
451
471
* :doc: `/bundles/configuration `
452
472
453
- .. _`PSR-0` : http://www.php-fig.org/psr/psr-0/
454
473
.. _`PSR-4` : http://www.php-fig.org/psr/psr-4/
474
+ .. _`Symfony Flex recipe` : https://github.com/symfony/recipes
455
475
.. _`Semantic Versioning Standard` : http://semver.org/
456
476
.. _`Packagist` : https://packagist.org/
457
477
.. _`choose any license` : http://choosealicense.com/
0 commit comments