Skip to content

Commit dfa9f33

Browse files
committed
minor symfony#6715 [Book] Remove DI extension info and link the cookbook article instead (WouterJ)
This PR was merged into the 2.7 branch. Discussion ---------- [Book] Remove DI extension info and link the cookbook article instead Imo, information about how to make your shared bundle load services doesn't belong in the book. I've replaced it with a shorter description and a link to the cookbook articles instead. Commits ------- 183e8be Remove complex DI extension information and link the cookbook article instead
2 parents 5863acb + 183e8be commit dfa9f33

File tree

1 file changed

+52
-101
lines changed

1 file changed

+52
-101
lines changed

book/service_container.rst

Lines changed: 52 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,10 @@ be imported from inside this file in one way or another. This gives you absolute
275275
flexibility over the services in your application.
276276

277277
External service configuration can be imported in two different ways. The first
278-
method, commonly used to import container configuration from the bundles you've
279-
created - is via the ``imports`` directive. The second method, although slightly more
280-
complex offers more flexibility and is commonly used to import third-party bundle
281-
configuration. Read on to learn more about both methods.
278+
method, commonly used to import other resources, is via the ``imports``
279+
directive. The second method, using dependency injection extensions, is used by
280+
third-party bundles to load the configuration. Read on to learn more about both
281+
methods.
282282

283283
.. index::
284284
single: Service Container; Imports
@@ -289,20 +289,16 @@ Importing Configuration with ``imports``
289289
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
290290

291291
So far, you've placed your ``app.mailer`` service container definition directly
292-
in the application configuration file (e.g. ``app/config/config.yml``). Of
293-
course, since the ``Mailer`` class itself lives inside the AppBundle, it
294-
makes more sense to put the ``app.mailer`` container definition inside the
295-
bundle as well.
296-
297-
First, move the ``app.mailer`` container definition into a new container resource
298-
file inside AppBundle. If the ``Resources`` or ``Resources/config``
299-
directories don't exist, create them.
292+
in the services configuration file (e.g. ``app/config/services.yml``). If your
293+
application ends up having many services, this file becomes huge and hard to
294+
maintain. To avoid this, you can split your service configuration into multiple
295+
service files:
300296

301297
.. configuration-block::
302298

303299
.. code-block:: yaml
304300
305-
# src/AppBundle/Resources/config/services.yml
301+
# app/config/services/mailer.yml
306302
parameters:
307303
app.mailer.transport: sendmail
308304
@@ -313,7 +309,7 @@ directories don't exist, create them.
313309
314310
.. code-block:: xml
315311
316-
<!-- src/AppBundle/Resources/config/services.xml -->
312+
<!-- app/config/services/mailer.xml -->
317313
<?xml version="1.0" encoding="UTF-8" ?>
318314
<container xmlns="http://symfony.com/schema/dic/services"
319315
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -333,7 +329,7 @@ directories don't exist, create them.
333329
334330
.. code-block:: php
335331
336-
// src/AppBundle/Resources/config/services.php
332+
// app/config/services/mailer.php
337333
use Symfony\Component\DependencyInjection\Definition;
338334
339335
$container->setParameter('app.mailer.transport', 'sendmail');
@@ -343,46 +339,42 @@ directories don't exist, create them.
343339
array('%app.mailer.transport%')
344340
));
345341
346-
The definition itself hasn't changed, only its location. Of course the service
347-
container doesn't know about the new resource file. Fortunately, you can
348-
easily import the resource file using the ``imports`` key in the application
349-
configuration.
342+
The definition itself hasn't changed, only its location. To make the service
343+
container load the definitions in this resource file, use the ``imports`` key
344+
in any already loaded resource (e.g. ``app/config/services.yml`` or
345+
``app/config/config.yml``):
350346

351347
.. configuration-block::
352348

353349
.. code-block:: yaml
354350
355-
# app/config/config.yml
351+
# app/config/services.yml
356352
imports:
357-
- { resource: '@AppBundle/Resources/config/services.yml' }
353+
- { resource: services/mailer.yml }
358354
359355
.. code-block:: xml
360356
361-
<!-- app/config/config.xml -->
357+
<!-- app/config/services.xml -->
362358
<?xml version="1.0" encoding="UTF-8" ?>
363359
<container xmlns="http://symfony.com/schema/dic/services"
364360
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
365361
xsi:schemaLocation="http://symfony.com/schema/dic/services
366362
http://symfony.com/schema/dic/services/services-1.0.xsd">
367363
368364
<imports>
369-
<import resource="@AppBundle/Resources/config/services.xml"/>
365+
<import resource="services/mailer.xml"/>
370366
</imports>
371367
</container>
372368
373369
.. code-block:: php
374370
375-
// app/config/config.php
376-
$loader->import('@AppBundle/Resources/config/services.php');
371+
// app/config/services.php
372+
$loader->import('services/mailer.php');
377373
378-
.. include:: /components/dependency_injection/_imports-parameters-note.rst.inc
374+
The ``resource`` location, for files, is either a relative path from the
375+
current file or an absolute path.
379376

380-
The ``imports`` directive allows your application to include service container
381-
configuration resources from any other location (most commonly from bundles).
382-
The ``resource`` location, for files, is the absolute path to the resource
383-
file. The special ``@AppBundle`` syntax resolves the directory path
384-
of the AppBundle bundle. This helps you specify the path to the resource
385-
without worrying later if you move the AppBundle to a different directory.
377+
.. include:: /components/dependency_injection/_imports-parameters-note.rst.inc
386378

387379
.. index::
388380
single: Service Container; Extension configuration
@@ -392,31 +384,14 @@ without worrying later if you move the AppBundle to a different directory.
392384
Importing Configuration via Container Extensions
393385
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
394386

395-
When developing in Symfony, you'll most commonly use the ``imports`` directive
396-
to import container configuration from the bundles you've created specifically
397-
for your application. Third-party bundle container configuration, including
398-
Symfony core services, are usually loaded using another method that's more
399-
flexible and easy to configure in your application.
400-
401-
Here's how it works. Internally, each bundle defines its services very much
402-
like you've seen so far. Namely, a bundle uses one or more configuration
403-
resource files (usually XML) to specify the parameters and services for that
404-
bundle. However, instead of importing each of these resources directly from
405-
your application configuration using the ``imports`` directive, you can simply
406-
invoke a *service container extension* inside the bundle that does the work for
407-
you. A service container extension is a PHP class created by the bundle author
408-
to accomplish two things:
409-
410-
* import all service container resources needed to configure the services for
411-
the bundle;
387+
Third-party bundle container configuration, including Symfony core services,
388+
are usually loaded using another method that's more flexible and easy to
389+
configure in your application.
412390

413-
* provide semantic, straightforward configuration so that the bundle can
414-
be configured without interacting with the flat parameters of the bundle's
415-
service container configuration.
416-
417-
In other words, a service container extension configures the services for
418-
a bundle on your behalf. And as you'll see in a moment, the extension provides
419-
a sensible, high-level interface for configuring the bundle.
391+
Internally, each bundle defines its services like you've seen so far. However,
392+
these files aren't imported using the ``import`` directive. These bundles use a
393+
*dependency injection extension* to load the files. The extension also allows
394+
bundles to provide configuration to dynamically load some services.
420395

421396
Take the FrameworkBundle - the core Symfony Framework bundle - as an
422397
example. The presence of the following code in your application configuration
@@ -428,10 +403,8 @@ invokes the service container extension inside the FrameworkBundle:
428403
429404
# app/config/config.yml
430405
framework:
431-
secret: xxxxxxxxxx
432-
form: true
433-
csrf_protection: true
434-
router: { resource: '%kernel.root_dir%/config/routing.yml' }
406+
secret: xxxxxxxxxx
407+
form: true
435408
# ...
436409
437410
.. code-block:: xml
@@ -441,15 +414,13 @@ invokes the service container extension inside the FrameworkBundle:
441414
<container xmlns="http://symfony.com/schema/dic/services"
442415
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
443416
xmlns:framework="http://symfony.com/schema/dic/symfony"
444-
xsi:schemaLocation="http://symfony.com/schema/dic/services
445-
http://symfony.com/schema/dic/services/services-1.0.xsd
446-
http://symfony.com/schema/dic/symfony
447-
http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
417+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
418+
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"
419+
>
448420
449421
<framework:config secret="xxxxxxxxxx">
450422
<framework:form />
451-
<framework:csrf-protection />
452-
<framework:router resource="%kernel.root_dir%/config/routing.xml" />
423+
453424
<!-- ... -->
454425
</framework>
455426
</container>
@@ -458,51 +429,31 @@ invokes the service container extension inside the FrameworkBundle:
458429
459430
// app/config/config.php
460431
$container->loadFromExtension('framework', array(
461-
'secret' => 'xxxxxxxxxx',
462-
'form' => array(),
463-
'csrf-protection' => array(),
464-
'router' => array(
465-
'resource' => '%kernel.root_dir%/config/routing.php',
466-
),
432+
'secret' => 'xxxxxxxxxx',
433+
'form' => array(),
467434
468435
// ...
469436
));
470437
471-
When the configuration is parsed, the container looks for an extension that
472-
can handle the ``framework`` configuration directive. The extension in question,
473-
which lives in the FrameworkBundle, is invoked and the service configuration
474-
for the FrameworkBundle is loaded. If you remove the ``framework`` key
475-
from your application configuration file entirely, the core Symfony services
476-
won't be loaded. The point is that you're in control: the Symfony Framework
477-
doesn't contain any magic or perform any actions that you don't have control
478-
over.
479-
480-
Of course you can do much more than simply "activate" the service container
481-
extension of the FrameworkBundle. Each extension allows you to easily
482-
customize the bundle, without worrying about how the internal services are
483-
defined.
484-
485-
In this case, the extension allows you to customize the ``error_handler``,
486-
``csrf_protection``, ``router`` configuration and much more. Internally,
487-
the FrameworkBundle uses the options specified here to define and configure
488-
the services specific to it. The bundle takes care of creating all the necessary
489-
``parameters`` and ``services`` for the service container, while still allowing
490-
much of the configuration to be easily customized. As a bonus, most
491-
service container extensions are also smart enough to perform validation -
492-
notifying you of options that are missing or the wrong data type.
438+
When the resources are parsed, the container looks for an extension that
439+
can handle the ``framework`` directive. The extension in question, which lives
440+
in the FrameworkBundle, is invoked and the service configuration for the
441+
FrameworkBundle is loaded.
442+
443+
The settings under the ``framework`` directive (e.g. ``form: true``) indicate
444+
that the extension should load all services related to the Form component. If
445+
form was disabled, these services wouldn't be loaded and Form integration would
446+
not be available.
493447

494448
When installing or configuring a bundle, see the bundle's documentation for
495449
how the services for the bundle should be installed and configured. The options
496450
available for the core bundles can be found inside the :doc:`Reference Guide </reference/index>`.
497451

498-
.. note::
499-
500-
Natively, the service container only recognizes the ``parameters``,
501-
``services``, and ``imports`` directives. Any other directives
502-
are handled by a service container extension.
452+
.. seealso::
503453

504-
If you want to expose user friendly configuration in your own bundles, read the
505-
":doc:`/cookbook/bundles/extension`" cookbook recipe.
454+
If you want to use dependency injection extensions in your own shared
455+
bundles and provide user friendly configuration, take a look at the
456+
":doc:`/cookbook/bundles/extension`" cookbook recipe.
506457

507458
.. index::
508459
single: Service Container; Referencing services

0 commit comments

Comments
 (0)