Skip to content

Commit c461ddf

Browse files
javiereguiluzxabbuh
authored andcommitted
Updated the bundle configuration articles to Symfony 4
1 parent 66bb046 commit c461ddf

File tree

2 files changed

+50
-106
lines changed

2 files changed

+50
-106
lines changed

bundles/configuration.rst

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
How to Create Friendly Configuration for a Bundle
66
=================================================
77

8-
If you open your application configuration file (usually ``app/config/config.yml``),
9-
you'll see a number of different configuration sections, such as ``framework``,
10-
``twig`` and ``doctrine``. Each of these configures a specific bundle, allowing
11-
you to define options at a high level and then let the bundle make all the
12-
low-level, complex changes based on your settings.
8+
If you open your main application configuration directory (usually
9+
``config/packages/``), you'll see a number of different files, such as
10+
``framework.yaml``, ``twig.yaml`` and ``doctrine.yaml``. Each of these
11+
configures a specific bundle, allowing you to define options at a high level and
12+
then let the bundle make all the low-level, complex changes based on your
13+
settings.
1314

1415
For example, the following configuration tells the FrameworkBundle to enable the
1516
form integration, which involves the definition of quite a few services as well
@@ -43,17 +44,6 @@ as integration of other related components:
4344
'form' => true,
4445
));
4546
46-
.. sidebar:: Using Parameters to Configure your Bundle
47-
48-
If you don't have plans to share your bundle between projects, it doesn't
49-
make sense to use this more advanced way of configuration. Since you use
50-
the bundle only in one project, you can just change the service
51-
configuration each time.
52-
53-
If you *do* want to be able to configure something from within
54-
``config.yml``, you can always create a parameter there and use that
55-
parameter somewhere else.
56-
5747
Using the Bundle Extension
5848
--------------------------
5949

@@ -71,15 +61,15 @@ bundle configuration would look like:
7161

7262
.. code-block:: yaml
7363
74-
# app/config/config.yml
64+
# config/packages/acme_social.yaml
7565
acme_social:
7666
twitter:
7767
client_id: 123
7868
client_secret: your_secret
7969
8070
.. code-block:: xml
8171
82-
<!-- app/config/config.xml -->
72+
<!-- config/packages/acme_social.xml -->
8373
<?xml version="1.0" ?>
8474
<container xmlns="http://symfony.com/schema/dic/services"
8575
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -96,7 +86,7 @@ bundle configuration would look like:
9686
9787
.. code-block:: php
9888
99-
// app/config/config.php
89+
// config/packages/acme_social.php
10090
$container->loadFromExtension('acme_social', array(
10191
'client_id' => 123,
10292
'client_secret' => 'your_secret',
@@ -145,20 +135,20 @@ For the configuration example in the previous section, the array passed to your
145135
)
146136

147137
Notice that this is an *array of arrays*, not just a single flat array of the
148-
configuration values. This is intentional, as it allows Symfony to parse
149-
several configuration resources. For example, if ``acme_social`` appears in
150-
another configuration file - say ``config_dev.yml`` - with different values
151-
beneath it, the incoming array might look like this::
138+
configuration values. This is intentional, as it allows Symfony to parse several
139+
configuration resources. For example, if ``acme_social`` appears in another
140+
configuration file - say ``config/packages/dev/acme_social.yaml`` - with
141+
different values beneath it, the incoming array might look like this::
152142

153143
array(
154-
// values from config.yml
144+
// values from config/packages/acme_social.yaml
155145
array(
156146
'twitter' => array(
157147
'client_id' => 123,
158148
'client_secret' => 'your_secret',
159149
),
160150
),
161-
// values from config_dev.yml
151+
// values from config/packages/dev/acme_social.yaml
162152
array(
163153
'twitter' => array(
164154
'client_id' => 456,
@@ -219,7 +209,6 @@ force validation (e.g. if an additional option was passed, an exception will be
219209
thrown)::
220210

221211
// src/Acme/SocialBundle/DependencyInjection/AcmeSocialExtension.php
222-
223212
public function load(array $configs, ContainerBuilder $container)
224213
{
225214
$configuration = new Configuration();
@@ -324,12 +313,10 @@ In your extension, you can load this and dynamically set its arguments::
324313
Modifying the Configuration of Another Bundle
325314
---------------------------------------------
326315

327-
If you have multiple bundles that depend on each other, it may be useful
328-
to allow one ``Extension`` class to modify the configuration passed to another
329-
bundle's ``Extension`` class, as if the end-developer has actually placed that
330-
configuration in their ``app/config/config.yml`` file. This can be achieved
331-
using a prepend extension. For more details, see
332-
:doc:`/bundles/prepend_extension`.
316+
If you have multiple bundles that depend on each other, it may be useful to
317+
allow one ``Extension`` class to modify the configuration passed to another
318+
bundle's ``Extension`` class. This can be achieved using a prepend extension.
319+
For more details, see :doc:`/bundles/prepend_extension`.
333320

334321
Dump the Configuration
335322
----------------------
@@ -401,7 +388,7 @@ namespace is then replaced with the XSD validation base path returned from
401388
method. This namespace is then followed by the rest of the path from the base
402389
path to the file itself.
403390

404-
By convention, the XSD file lives in the ``Resources/config/schema``, but you
391+
By convention, the XSD file lives in the ``Resources/config/schema/``, but you
405392
can place it anywhere you like. You should return this path as the base path::
406393

407394
// src/Acme/HelloBundle/DependencyInjection/AcmeHelloExtension.php
@@ -422,7 +409,7 @@ Assuming the XSD file is called ``hello-1.0.xsd``, the schema location will be
422409

423410
.. code-block:: xml
424411
425-
<!-- app/config/config.xml -->
412+
<!-- config/packages/acme_hello.xml -->
426413
<?xml version="1.0" ?>
427414
<container xmlns="http://symfony.com/schema/dic/services"
428415
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

bundles/extension.rst

Lines changed: 29 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,29 @@
55
How to Load Service Configuration inside a Bundle
66
=================================================
77

8-
In Symfony, you'll find yourself using many services. These services can be
9-
registered in the ``app/config/`` directory of your application. But when you
10-
want to decouple the bundle for use in other projects, you want to include the
11-
service configuration in the bundle itself. This article will teach you how to
12-
do that.
8+
Services created by bundles are not defined in the main ``config/services.yaml``
9+
file used by the application but in the bundles themselves. This article
10+
explains how to create and load those bundle services files.
1311

1412
Creating an Extension Class
1513
---------------------------
1614

1715
In order to load service configuration, you have to create a Dependency
18-
Injection (DI) Extension for your bundle. This class has some conventions in order
19-
to be detected automatically. But you'll later see how you can change it to
20-
your own preferences. By default, the Extension has to comply with the
21-
following conventions:
16+
Injection (DI) Extension for your bundle. By default, the Extension class must
17+
follow these conventions (but later you'll learn how to skip them if needed):
2218

2319
* It has to live in the ``DependencyInjection`` namespace of the bundle;
2420

21+
* It has to implement the :class:`Symfony\\Component\\DependencyInjection\\Extension\\ExtensionInterface`,
22+
which is usually achieve by extending the
23+
:class:`Symfony\\Component\\DependencyInjection\\Extension\\Extension` class;
24+
2525
* The name is equal to the bundle name with the ``Bundle`` suffix replaced by
26-
``Extension`` (e.g. the Extension class of the AppBundle would be called
27-
``AppExtension`` and the one for AcmeHelloBundle would be called
26+
``Extension`` (e.g. the Extension class of the AcmeBundle would be called
27+
``AcmeExtension`` and the one for AcmeHelloBundle would be called
2828
``AcmeHelloExtension``).
2929

30-
The Extension class should implement the
31-
:class:`Symfony\\Component\\DependencyInjection\\Extension\\ExtensionInterface`,
32-
but usually you would simply extend the
33-
:class:`Symfony\\Component\\DependencyInjection\\Extension\\Extension` class::
30+
This is how the extension of an AcmeHelloBundle should look like::
3431

3532
// src/Acme/HelloBundle/DependencyInjection/AcmeHelloExtension.php
3633
namespace Acme\HelloBundle\DependencyInjection;
@@ -65,11 +62,11 @@ method to return the instance of the extension::
6562
}
6663
}
6764

68-
Since the new Extension class name doesn't follow the naming conventions, you
69-
should also override
65+
In addition, when the new Extension class name doesn't follow the naming
66+
conventions, you must also override the
7067
:method:`Extension::getAlias() <Symfony\\Component\\DependencyInjection\\Extension\\Extension::getAlias>`
71-
to return the correct DI alias. The DI alias is the name used to refer to the
72-
bundle in the container (e.g. in the ``app/config/config.yml`` file). By
68+
method to return the correct DI alias. The DI alias is the name used to refer to
69+
the bundle in the container (e.g. in the ``config/packages/`` files). By
7370
default, this is done by removing the ``Extension`` suffix and converting the
7471
class name to underscores (e.g. ``AcmeHelloExtension``'s DI alias is
7572
``acme_hello``).
@@ -86,11 +83,10 @@ container.
8683

8784
In the ``load()`` method, you can use PHP code to register service definitions,
8885
but it is more common if you put these definitions in a configuration file
89-
(using the Yaml, XML or PHP format). Luckily, you can use the file loaders in
90-
the extension!
86+
(using the YAML, XML or PHP format).
9187

9288
For instance, assume you have a file called ``services.xml`` in the
93-
``Resources/config`` directory of your bundle, your ``load()`` method looks like::
89+
``Resources/config/`` directory of your bundle, your ``load()`` method looks like::
9490

9591
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
9692
use Symfony\Component\Config\FileLocator;
@@ -105,39 +101,22 @@ For instance, assume you have a file called ``services.xml`` in the
105101
$loader->load('services.xml');
106102
}
107103

108-
Other available loaders are the ``YamlFileLoader``, ``PhpFileLoader`` and
109-
``IniFileLoader``.
110-
111-
.. note::
112-
113-
The ``IniFileLoader`` can only be used to load parameters and it can only
114-
load them as strings.
115-
116-
.. caution::
117-
118-
If you removed the default file with service definitions (i.e.
119-
``config/services.yaml``), make sure to also remove it from the
120-
``imports`` key in ``app/config/config.yml``.
104+
The other available loaders are ``YamlFileLoader`` and ``PhpFileLoader``.
121105

122106
Using Configuration to Change the Services
123107
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
124108

125109
The Extension is also the class that handles the configuration for that
126-
particular bundle (e.g. the configuration in ``app/config/config.yml``). To
127-
read more about it, see the ":doc:`/bundles/configuration`" article.
110+
particular bundle (e.g. the configuration in ``config/packages/<bundle_alias>.yaml``).
111+
To read more about it, see the ":doc:`/bundles/configuration`" article.
128112

129113
Adding Classes to Compile
130114
-------------------------
131115

132-
Symfony creates a big ``classes.php`` file in the cache directory to aggregate
133-
the contents of the PHP classes that are used in every request. This reduces the
134-
I/O operations and increases the application performance.
135-
136-
Your bundles can also add their own classes into this file thanks to the
137-
``addClassesToCompile()`` and ``addAnnotatedClassesToCompile()`` methods (both
138-
work in the same way, but the second one is for classes that contain PHP
139-
annotations). Define the classes to compile as an array of their fully qualified
140-
class names::
116+
Bundles can hint Symfony about which of their classes contain annotations so
117+
they are compiled when generating the application cache to improve the overall
118+
performance. Define the list of annotated classes to compile in the
119+
``addAnnotatedClassesToCompile()`` method::
141120

142121
use App\Manager\UserManager;
143122
use App\Utils\Slugger;
@@ -147,16 +126,12 @@ class names::
147126
{
148127
// ...
149128

150-
// this method can't compile classes that contain PHP annotations
151-
$this->addClassesToCompile(array(
152-
UserManager::class,
153-
Slugger::class,
154-
// ...
155-
));
156-
157-
// add here only classes that contain PHP annotations
158129
$this->addAnnotatedClassesToCompile(array(
130+
// you can define the fully qualified class names...
159131
'App\\Controller\\DefaultController',
132+
// ... but glob patterns are also supported:
133+
'**Bundle\\Controller\\',
134+
160135
// ...
161136
));
162137
}
@@ -166,24 +141,6 @@ class names::
166141
If some class extends from other classes, all its parents are automatically
167142
included in the list of classes to compile.
168143

169-
The classes to compile can also be added using file path patterns::
170-
171-
// ...
172-
public function load(array $configs, ContainerBuilder $container)
173-
{
174-
// ...
175-
176-
$this->addClassesToCompile(array(
177-
'**Bundle\\Manager\\',
178-
// ...
179-
));
180-
181-
$this->addAnnotatedClassesToCompile(array(
182-
'**Bundle\\Controller\\',
183-
// ...
184-
));
185-
}
186-
187144
Patterns are transformed into the actual class namespaces using the classmap
188145
generated by Composer. Therefore, before using these patterns, you must generate
189146
the full classmap executing the ``dump-autoload`` command of Composer.

0 commit comments

Comments
 (0)