Skip to content

Commit 0b6e53c

Browse files
committed
Promoting new bundle directory structure as best practice
1 parent 1839389 commit 0b6e53c

File tree

1 file changed

+61
-38
lines changed

1 file changed

+61
-38
lines changed

bundles/best_practices.rst

Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -68,30 +68,41 @@ The basic directory structure of an AcmeBlogBundle must read as follows:
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.rst
74+
├── public/
75+
├── src/
76+
│ ├── Controller/
77+
│ ├── DependencyInjection/
78+
│ └── AcmeBlogBundle.php
79+
├── templates/
80+
├── tests/
81+
├── translations/
82+
├── LICENSE
83+
└── README.md
84+
85+
and the bundle path must be adjusted to the root directory::
86+
87+
class AcmeBlogBundle extends Bundle
88+
{
89+
public function getPath(): string
90+
{
91+
return \dirname(__DIR__);
92+
}
93+
}
8394

8495
**The following files are mandatory**, because they ensure a structure convention
8596
that automated tools can rely on:
8697

87-
* ``AcmeBlogBundle.php``: This is the class that transforms a plain directory
98+
* ``src/AcmeBlogBundle.php``: This is the class that transforms a plain directory
8899
into a Symfony bundle (change this to your bundle's name);
89100
* ``README.md``: This file contains the basic description of the bundle and it
90101
usually shows some basic examples and links to its full documentation (it
91102
can use any of the markup formats supported by GitHub, such as ``README.rst``);
92103
* ``LICENSE``: The full contents of the license used by the code. Most third-party
93104
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.
105+
* ``docs/index.rst``: The root file for the Bundle documentation.
95106

96107
The depth of subdirectories should be kept to a minimum for the most used
97108
classes and files. Two levels is the maximum.
@@ -107,27 +118,27 @@ and others are just conventions followed by most developers):
107118
=================================================== ========================================
108119
Type Directory
109120
=================================================== ========================================
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/``
121+
Commands ``src/Command/``
122+
Controllers ``src/Controller/``
123+
Service Container Extensions ``src/DependencyInjection/``
124+
Doctrine ORM entities (when not using annotations) ``src/Entity/``
125+
Doctrine ODM documents (when not using annotations) ``src/Document/``
126+
Event Listeners ``src/EventListener/``
127+
Configuration (routes, services, etc.) ``config/``
128+
Web Assets (CSS, JS, images) ``public/``
129+
Translation files ``translations/``
130+
Validation (when not using annotations) ``config/validation/``
131+
Serialization (when not using annotations) ``config/serialization/``
132+
Templates ``templates/``
133+
Unit and Functional Tests ``tests/``
123134
=================================================== ========================================
124135

125136
Classes
126137
-------
127138

128139
The bundle directory structure is used as the namespace hierarchy. For
129140
instance, a ``ContentController`` controller which is stored in
130-
``Acme/BlogBundle/Controller/ContentController.php`` would have the fully
141+
``Acme/BlogBundle/src/Controller/ContentController.php`` would have the fully
131142
qualified class name of ``Acme\BlogBundle\Controller\ContentController``.
132143

133144
All classes and files must follow the :doc:`Symfony coding standards </contributing/code/standards>`.
@@ -153,7 +164,7 @@ Tests
153164
-----
154165

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

158169
* The test suite must be executable with a simple ``phpunit`` command run from
159170
a sample application;
@@ -240,10 +251,10 @@ Documentation
240251

241252
All classes and functions must come with full PHPDoc.
242253

243-
Extensive documentation should also be provided in the ``Resources/doc/``
254+
Extensive documentation should also be provided in the ``docs/``
244255
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
256+
The index file (for example ``docs/index.rst`` or
257+
``docs/index.md``) is the only mandatory file and must be the entry
247258
point for the documentation. The
248259
:doc:`reStructuredText (rST) </contributing/documentation/format>` is the format
249260
used to render the documentation on the Symfony website.
@@ -480,10 +491,22 @@ The ``composer.json`` file should include at least the following metadata:
480491
This information is used by Symfony to load the classes of the bundle. It's
481492
recommended to use the `PSR-4`_ autoload standard: use the namespace as key,
482493
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/" } }``.
494+
as value. As the main class is located in the ``src/`` directory of the bundle:
495+
496+
.. code-block:: json
497+
498+
{
499+
"autoload": {
500+
"psr-4": {
501+
"SomeVendor\\BlogBundle\\": "src/"
502+
}
503+
},
504+
"autoload-dev": {
505+
"psr-4": {
506+
"SomeVendor\\BlogBundle\\Tests\\": "tests/"
507+
}
508+
}
509+
}
487510
488511
In order to make it easier for developers to find your bundle, register it on
489512
`Packagist`_, the official repository for Composer packages.
@@ -493,14 +516,14 @@ Resources
493516

494517
If the bundle references any resources (config files, translation files, etc.),
495518
don't use physical paths (e.g. ``__DIR__/config/services.xml``) but logical
496-
paths (e.g. ``@FooBundle/Resources/config/services.xml``).
519+
paths (e.g. ``@FooBundle/config/services.xml``).
497520

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

502525
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/``
526+
For example, an ``index.html.twig`` template located in the ``templates/Default/``
504527
directory of the FooBundle, is referenced as ``@Foo/Default/index.html.twig``.
505528

506529
Learn more

0 commit comments

Comments
 (0)