Skip to content

Commit 34ddce5

Browse files
committed
minor #19185 [Bundle] Sync doc pages with current Symfony codebase (smnandre)
This PR was submitted for the 6.4 branch but it was squashed and merged into the 6.3 branch instead. Discussion ---------- [Bundle] Sync doc pages with current Symfony codebase A group of updates related to bundles, mainly about namespaces, source paths and code sample sync with the current Symfony codebase / best practices. I won't mind a second look :) Commits ------- 2d0af9c [Bundle] Sync doc pages with current Symfony codebase
2 parents b003eaf + 2d0af9c commit 34ddce5

File tree

4 files changed

+54
-49
lines changed

4 files changed

+54
-49
lines changed

bundles.rst

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ file::
2222
return [
2323
// 'all' means that the bundle is enabled for any Symfony environment
2424
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
25-
Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true],
26-
Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
27-
Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true],
28-
Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
29-
Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle::class => ['all' => true],
25+
// ...
26+
27+
// this bundle is enabled only in 'dev'
28+
Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true],
29+
// ...
30+
3031
// this bundle is enabled only in 'dev' and 'test', so you can't use it in 'prod'
3132
Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],
33+
// ...
3234
];
3335

3436
.. tip::
@@ -41,18 +43,18 @@ Creating a Bundle
4143
-----------------
4244

4345
This section creates and enables a new bundle to show there are only a few steps required.
44-
The new bundle is called AcmeTestBundle, where the ``Acme`` portion is an example
46+
The new bundle is called AcmeBlogBundle, where the ``Acme`` portion is an example
4547
name that should be replaced by some "vendor" name that represents you or your
46-
organization (e.g. AbcTestBundle for some company named ``Abc``).
48+
organization (e.g. AbcBlogBundle for some company named ``Abc``).
4749

48-
Start by creating a new class called ``AcmeTestBundle``::
50+
Start by creating a new class called ``AcmeBlogBundle``::
4951

50-
// src/AcmeTestBundle.php
51-
namespace Acme\TestBundle;
52+
// src/AcmeBlogBundle.php
53+
namespace Acme\BlogBundle;
5254

5355
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
5456

55-
class AcmeTestBundle extends AbstractBundle
57+
class AcmeBlogBundle extends AbstractBundle
5658
{
5759
}
5860

@@ -68,10 +70,10 @@ Start by creating a new class called ``AcmeTestBundle``::
6870

6971
.. tip::
7072

71-
The name AcmeTestBundle follows the standard
73+
The name AcmeBlogBundle follows the standard
7274
:ref:`Bundle naming conventions <bundles-naming-conventions>`. You could
73-
also choose to shorten the name of the bundle to simply TestBundle by naming
74-
this class TestBundle (and naming the file ``TestBundle.php``).
75+
also choose to shorten the name of the bundle to simply BlogBundle by naming
76+
this class BlogBundle (and naming the file ``BlogBundle.php``).
7577

7678
This empty class is the only piece you need to create the new bundle. Though
7779
commonly empty, this class is powerful and can be used to customize the behavior
@@ -80,10 +82,10 @@ of the bundle. Now that you've created the bundle, enable it::
8082
// config/bundles.php
8183
return [
8284
// ...
83-
Acme\TestBundle\AcmeTestBundle::class => ['all' => true],
85+
Acme\BlogBundle\AcmeBlogBundle::class => ['all' => true],
8486
];
8587

86-
And while it doesn't do anything yet, AcmeTestBundle is now ready to be used.
88+
And while it doesn't do anything yet, AcmeBlogBundle is now ready to be used.
8789

8890
Bundle Directory Structure
8991
--------------------------
@@ -92,31 +94,31 @@ The directory structure of a bundle is meant to help to keep code consistent
9294
between all Symfony bundles. It follows a set of conventions, but is flexible
9395
to be adjusted if needed:
9496

95-
``src/``
96-
Contains all PHP classes related to the bundle logic (e.g. ``Controller/RandomController.php``).
97+
``assets/``
98+
Contains the web asset sources like JavaScript and TypeScript files, CSS and
99+
Sass files, but also images and other assets related to the bundle that are
100+
not in ``public/`` (e.g. Stimulus controllers).
97101

98102
``config/``
99-
Houses configuration, including routing configuration (e.g. ``routing.yaml``).
100-
101-
``templates/``
102-
Holds templates organized by controller name (e.g. ``random/index.html.twig``).
103-
104-
``translations/``
105-
Holds translations organized by domain and locale (e.g. ``AcmeTestBundle.en.xlf``).
103+
Houses configuration, including routing configuration (e.g. ``routes.php``).
106104

107105
``public/``
108106
Contains web assets (images, compiled CSS and JavaScript files, etc.) and is
109107
copied or symbolically linked into the project ``public/`` directory via the
110108
``assets:install`` console command.
111109

112-
``assets/``
113-
Contains the web asset sources (JavaScript and TypeScript files, CSS and Sass
114-
files, etc.), images and other assets related to the bundle that are not in
115-
``public/`` (e.g. Stimulus controllers)
110+
``src/``
111+
Contains all PHP classes related to the bundle logic (e.g. ``Controller/CategoryController.php``).
112+
113+
``templates/``
114+
Holds templates organized by controller name (e.g. ``category/show.html.twig``).
116115

117116
``tests/``
118117
Holds all tests for the bundle.
119118

119+
``translations/``
120+
Holds translations organized by domain and locale (e.g. ``AcmeBlogBundle.en.xlf``).
121+
120122
.. caution::
121123

122124
The recommended bundle structure was changed in Symfony 5, read the
@@ -127,7 +129,7 @@ to be adjusted if needed:
127129
new structure. Override the ``Bundle::getPath()`` method to change to
128130
the old structure::
129131

130-
class AcmeTestBundle extends AbstractBundle
132+
class AcmeBlogBundle extends AbstractBundle
131133
{
132134
public function getPath(): string
133135
{
@@ -146,12 +148,12 @@ to be adjusted if needed:
146148
{
147149
"autoload": {
148150
"psr-4": {
149-
"Acme\\TestBundle\\": "src/"
151+
"Acme\\BlogBundle\\": "src/"
150152
}
151153
},
152154
"autoload-dev": {
153155
"psr-4": {
154-
"Acme\\TestBundle\\Tests\\": "tests/"
156+
"Acme\\BlogBundle\\Tests\\": "tests/"
155157
}
156158
}
157159
}

bundles/configuration.rst

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ of your bundle's configuration.
175175

176176
The ``Configuration`` class to handle the sample configuration looks like::
177177

178-
// src/Acme/SocialBundle/DependencyInjection/Configuration.php
178+
// src/DependencyInjection/Configuration.php
179179
namespace Acme\SocialBundle\DependencyInjection;
180180

181181
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
@@ -216,7 +216,7 @@ This class can now be used in your ``load()`` method to merge configurations and
216216
force validation (e.g. if an additional option was passed, an exception will be
217217
thrown)::
218218

219-
// src/Acme/SocialBundle/DependencyInjection/AcmeSocialExtension.php
219+
// src/DependencyInjection/AcmeSocialExtension.php
220220
public function load(array $configs, ContainerBuilder $container): void
221221
{
222222
$configuration = new Configuration();
@@ -236,7 +236,7 @@ For example, imagine your bundle has the following example config:
236236

237237
.. code-block:: xml
238238
239-
<!-- src/Acme/SocialBundle/Resources/config/services.xml -->
239+
<!-- src/config/services.xml -->
240240
<?xml version="1.0" encoding="UTF-8" ?>
241241
<container xmlns="http://symfony.com/schema/dic/services"
242242
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -253,8 +253,8 @@ For example, imagine your bundle has the following example config:
253253
254254
In your extension, you can load this and dynamically set its arguments::
255255

256-
// src/Acme/SocialBundle/DependencyInjection/AcmeSocialExtension.php
257-
// ...
256+
// src/DependencyInjection/AcmeSocialExtension.php
257+
namespace Acme\SocialBundle\DependencyInjection;
258258

259259
use Symfony\Component\Config\FileLocator;
260260
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
@@ -279,7 +279,7 @@ In your extension, you can load this and dynamically set its arguments::
279279
:class:`Symfony\\Component\\HttpKernel\\DependencyInjection\\ConfigurableExtension`
280280
to do this automatically for you::
281281

282-
// src/Acme/HelloBundle/DependencyInjection/AcmeHelloExtension.php
282+
// src/DependencyInjection/HelloExtension.php
283283
namespace Acme\HelloBundle\DependencyInjection;
284284

285285
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -375,6 +375,7 @@ logic to the bundle class directly::
375375
configuration definition from one or more files::
376376

377377
// src/AcmeSocialBundle.php
378+
namespace Acme\SocialBundle;
378379

379380
// ...
380381
class AcmeSocialBundle extends AbstractBundle
@@ -417,7 +418,7 @@ The ``config:dump-reference`` command dumps the default configuration of a
417418
bundle in the console using the Yaml format.
418419

419420
As long as your bundle's configuration is located in the standard location
420-
(``YourBundle\DependencyInjection\Configuration``) and does not have
421+
(``<YourBundle>/src/DependencyInjection/Configuration``) and does not have
421422
a constructor, it will work automatically. If you
422423
have something different, your ``Extension`` class must override the
423424
:method:`Extension::getConfiguration() <Symfony\\Component\\DependencyInjection\\Extension\\Extension::getConfiguration>`
@@ -451,7 +452,8 @@ URL nor does it need to exist). By default, the namespace for a bundle is
451452
``http://example.org/schema/dic/DI_ALIAS``, where ``DI_ALIAS`` is the DI alias of
452453
the extension. You might want to change this to a more professional URL::
453454

454-
// src/Acme/HelloBundle/DependencyInjection/AcmeHelloExtension.php
455+
// src/DependencyInjection/AcmeHelloExtension.php
456+
namespace Acme\HelloBundle\DependencyInjection;
455457

456458
// ...
457459
class AcmeHelloExtension extends Extension
@@ -480,10 +482,11 @@ namespace is then replaced with the XSD validation base path returned from
480482
method. This namespace is then followed by the rest of the path from the base
481483
path to the file itself.
482484

483-
By convention, the XSD file lives in the ``Resources/config/schema/``, but you
485+
By convention, the XSD file lives in ``config/schema/`` directory, but you
484486
can place it anywhere you like. You should return this path as the base path::
485487

486-
// src/Acme/HelloBundle/DependencyInjection/AcmeHelloExtension.php
488+
// src/DependencyInjection/AcmeHelloExtension.php
489+
namespace Acme\HelloBundle\DependencyInjection;
487490

488491
// ...
489492
class AcmeHelloExtension extends Extension
@@ -492,7 +495,7 @@ can place it anywhere you like. You should return this path as the base path::
492495

493496
public function getXsdValidationBasePath(): string
494497
{
495-
return __DIR__.'/../Resources/config/schema';
498+
return __DIR__.'/../config/schema';
496499
}
497500
}
498501

bundles/extension.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ performance. Define the list of annotated classes to compile in the
173173

174174
$this->addAnnotatedClassesToCompile([
175175
// you can define the fully qualified class names...
176-
'App\\Controller\\DefaultController',
176+
'Acme\\BlogBundle\\Controller\\AuthorController',
177177
// ... but glob patterns are also supported:
178-
'**Bundle\\Controller\\',
178+
'Acme\\BlogBundle\\Form\\**',
179179

180180
// ...
181181
]);

templates.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,10 +1417,10 @@ may include their own Twig templates (in the ``Resources/views/`` directory of
14171417
each bundle). To avoid messing with your own templates, Symfony adds bundle
14181418
templates under an automatic namespace created after the bundle name.
14191419

1420-
For example, the templates of a bundle called ``AcmeFooBundle`` are available
1421-
under the ``AcmeFoo`` namespace. If this bundle includes the template
1422-
``<your-project>/vendor/acmefoo-bundle/Resources/views/user/profile.html.twig``,
1423-
you can refer to it as ``@AcmeFoo/user/profile.html.twig``.
1420+
For example, the templates of a bundle called ``AcmeBlogBundle`` are available
1421+
under the ``AcmeBlog`` namespace. If this bundle includes the template
1422+
``<your-project>/vendor/acme/blog-bundle/Resources/views/user/profile.html.twig``,
1423+
you can refer to it as ``@AcmeBlog/user/profile.html.twig``.
14241424

14251425
.. tip::
14261426

0 commit comments

Comments
 (0)