Skip to content

Commit 8e8c6d8

Browse files
committed
Merge branch '5.2' into 5.x
* 5.2: Promoting new bundle directory structure as best practice
2 parents af309bd + a85c441 commit 8e8c6d8

File tree

1 file changed

+69
-40
lines changed

1 file changed

+69
-40
lines changed

bundles/best_practices.rst

Lines changed: 69 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -63,35 +63,52 @@ configuration options (see below for some usage examples).
6363
Directory Structure
6464
-------------------
6565

66-
The basic directory structure of an AcmeBlogBundle must read as follows:
66+
The following is the recommended directory structure of an AcmeBlogBundle:
6767

6868
.. code-block:: text
6969
7070
<your-bundle>/
71-
├─ AcmeBlogBundle.php
72-
├─ Controller/
73-
├─ README.md
74-
├─ LICENSE
75-
├─ Resources/
76-
│ ├─ config/
77-
│ ├─ doc/
78-
│ │ └─ index.rst
79-
│ ├─ translations/
80-
│ ├─ views/
81-
│ └─ public/
82-
└─ Tests/
71+
├── config/
72+
├── docs/
73+
│ └─ index.md
74+
├── public/
75+
├── src/
76+
│ ├── Controller/
77+
│ ├── DependencyInjection/
78+
│ └── AcmeBlogBundle.php
79+
├── templates/
80+
├── tests/
81+
├── translations/
82+
├── LICENSE
83+
└── README.md
84+
85+
.. versionadded:: 4.4
86+
87+
This directory convention was introduced in Symfony 4.4 and can be used only
88+
when requiring ``symfony/http-kernel`` 4.4 or superior.
89+
90+
This directory structure requires to configure the bundle path to its root
91+
directory as follows::
92+
93+
class AcmeBlogBundle extends Bundle
94+
{
95+
public function getPath(): string
96+
{
97+
return \dirname(__DIR__);
98+
}
99+
}
83100

84101
**The following files are mandatory**, because they ensure a structure convention
85102
that automated tools can rely on:
86103

87-
* ``AcmeBlogBundle.php``: This is the class that transforms a plain directory
104+
* ``src/AcmeBlogBundle.php``: This is the class that transforms a plain directory
88105
into a Symfony bundle (change this to your bundle's name);
89106
* ``README.md``: This file contains the basic description of the bundle and it
90107
usually shows some basic examples and links to its full documentation (it
91108
can use any of the markup formats supported by GitHub, such as ``README.rst``);
92109
* ``LICENSE``: The full contents of the license used by the code. Most third-party
93110
bundles are published under the MIT license, but you can `choose any license`_;
94-
* ``Resources/doc/index.rst``: The root file for the Bundle documentation.
111+
* ``docs/index.md``: The root file for the Bundle documentation.
95112

96113
The depth of subdirectories should be kept to a minimum for the most used
97114
classes and files. Two levels is the maximum.
@@ -107,27 +124,27 @@ and others are just conventions followed by most developers):
107124
=================================================== ========================================
108125
Type Directory
109126
=================================================== ========================================
110-
Commands ``Command/``
111-
Controllers ``Controller/``
112-
Service Container Extensions ``DependencyInjection/``
113-
Doctrine ORM entities (when not using annotations) ``Entity/``
114-
Doctrine ODM documents (when not using annotations) ``Document/``
115-
Event Listeners ``EventListener/``
116-
Configuration (routes, services, etc.) ``Resources/config/``
117-
Web Assets (CSS, JS, images) ``Resources/public/``
118-
Translation files ``Resources/translations/``
119-
Validation (when not using annotations) ``Resources/config/validation/``
120-
Serialization (when not using annotations) ``Resources/config/serialization/``
121-
Templates ``Resources/views/``
122-
Unit and Functional Tests ``Tests/``
127+
Commands ``src/Command/``
128+
Controllers ``src/Controller/``
129+
Service Container Extensions ``src/DependencyInjection/``
130+
Doctrine ORM entities (when not using annotations) ``src/Entity/``
131+
Doctrine ODM documents (when not using annotations) ``src/Document/``
132+
Event Listeners ``src/EventListener/``
133+
Configuration (routes, services, etc.) ``config/``
134+
Web Assets (CSS, JS, images) ``public/``
135+
Translation files ``translations/``
136+
Validation (when not using annotations) ``config/validation/``
137+
Serialization (when not using annotations) ``config/serialization/``
138+
Templates ``templates/``
139+
Unit and Functional Tests ``tests/``
123140
=================================================== ========================================
124141

125142
Classes
126143
-------
127144

128145
The bundle directory structure is used as the namespace hierarchy. For
129146
instance, a ``ContentController`` controller which is stored in
130-
``Acme/BlogBundle/Controller/ContentController.php`` would have the fully
147+
``src/Controller/ContentController.php`` would have the fully
131148
qualified class name of ``Acme\BlogBundle\Controller\ContentController``.
132149

133150
All classes and files must follow the :doc:`Symfony coding standards </contributing/code/standards>`.
@@ -153,7 +170,7 @@ Tests
153170
-----
154171

155172
A bundle should come with a test suite written with PHPUnit and stored under
156-
the ``Tests/`` directory. Tests should follow the following principles:
173+
the ``tests/`` directory. Tests should follow the following principles:
157174

158175
* The test suite must be executable with a simple ``phpunit`` command run from
159176
a sample application;
@@ -240,10 +257,10 @@ Documentation
240257

241258
All classes and functions must come with full PHPDoc.
242259

243-
Extensive documentation should also be provided in the ``Resources/doc/``
260+
Extensive documentation should also be provided in the ``docs/``
244261
directory.
245-
The index file (for example ``Resources/doc/index.rst`` or
246-
``Resources/doc/index.md``) is the only mandatory file and must be the entry
262+
The index file (for example ``docs/index.rst`` or
263+
``docs/index.md``) is the only mandatory file and must be the entry
247264
point for the documentation. The
248265
:doc:`reStructuredText (rST) </contributing/documentation/format>` is the format
249266
used to render the documentation on the Symfony website.
@@ -480,10 +497,22 @@ The ``composer.json`` file should include at least the following metadata:
480497
This information is used by Symfony to load the classes of the bundle. It's
481498
recommended to use the `PSR-4`_ autoload standard: use the namespace as key,
482499
and the location of the bundle's main class (relative to ``composer.json``)
483-
as value. For example, if the main class is located in the bundle root
484-
directory: ``"autoload": { "psr-4": { "SomeVendor\\BlogBundle\\": "" } }``.
485-
If the main class is located in the ``src/`` directory of the bundle:
486-
``"autoload": { "psr-4": { "SomeVendor\\BlogBundle\\": "src/" } }``.
500+
as value. As the main class is located in the ``src/`` directory of the bundle:
501+
502+
.. code-block:: json
503+
504+
{
505+
"autoload": {
506+
"psr-4": {
507+
"Acme\\BlogBundle\\": "src/"
508+
}
509+
},
510+
"autoload-dev": {
511+
"psr-4": {
512+
"Acme\\BlogBundle\\Tests\\": "tests/"
513+
}
514+
}
515+
}
487516
488517
In order to make it easier for developers to find your bundle, register it on
489518
`Packagist`_, the official repository for Composer packages.
@@ -493,15 +522,15 @@ Resources
493522

494523
If the bundle references any resources (config files, translation files, etc.),
495524
don't use physical paths (e.g. ``__DIR__/config/services.xml``) but logical
496-
paths (e.g. ``@FooBundle/Resources/config/services.xml``).
525+
paths (e.g. ``@AcmeBlogBundle/config/services.xml``).
497526

498527
The logical paths are required because of the bundle overriding mechanism that
499528
lets you override any resource/file of any bundle. See :ref:`http-kernel-resource-locator`
500529
for more details about transforming physical paths into logical paths.
501530

502531
Beware that templates use a simplified version of the logical path shown above.
503-
For example, an ``index.html.twig`` template located in the ``Resources/views/Default/``
504-
directory of the FooBundle, is referenced as ``@Foo/Default/index.html.twig``.
532+
For example, an ``index.html.twig`` template located in the ``templates/Default/``
533+
directory of the AcmeBlogBundle, is referenced as ``@AcmeBlog/Default/index.html.twig``.
505534

506535
Learn more
507536
----------

0 commit comments

Comments
 (0)