@@ -277,10 +277,10 @@ be imported from inside this file in one way or another. This gives you absolute
277
277
flexibility over the services in your application.
278
278
279
279
External service configuration can be imported in two different ways. The first
280
- method, commonly used to import container configuration from the bundles you've
281
- created - is via the `` imports `` directive. The second method, although slightly more
282
- complex offers more flexibility and is commonly used to import third-party bundle
283
- configuration. Read on to learn more about both methods.
280
+ method, commonly used to import other resources, is via the `` imports ``
281
+ directive. The second method, using dependency injection extensions, is used by
282
+ third-party bundles to load the configuration. Read on to learn more about both
283
+ methods.
284
284
285
285
.. index ::
286
286
single: Service Container; Imports
@@ -291,20 +291,16 @@ Importing Configuration with ``imports``
291
291
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
292
292
293
293
So far, you've placed your ``app.mailer `` service container definition directly
294
- in the application configuration file (e.g. ``app/config/config.yml ``). Of
295
- course, since the ``Mailer `` class itself lives inside the AppBundle, it
296
- makes more sense to put the ``app.mailer `` container definition inside the
297
- bundle as well.
298
-
299
- First, move the ``app.mailer `` container definition into a new container resource
300
- file inside AppBundle. If the ``Resources `` or ``Resources/config ``
301
- directories don't exist, create them.
294
+ in the services configuration file (e.g. ``app/config/services.yml ``). If your
295
+ application ends up having many services, this file becomes huge and hard to
296
+ maintain. To avoid this, you can split your service configuration into multiple
297
+ service files:
302
298
303
299
.. configuration-block ::
304
300
305
301
.. code-block :: yaml
306
302
307
- # src/AppBundle/Resources/ config/services.yml
303
+ # app/ config/services/mailer .yml
308
304
parameters :
309
305
app.mailer.transport : sendmail
310
306
@@ -315,7 +311,7 @@ directories don't exist, create them.
315
311
316
312
.. code-block :: xml
317
313
318
- <!-- src/AppBundle/Resources/ config/services.xml -->
314
+ <!-- app/ config/services/mailer .xml -->
319
315
<?xml version =" 1.0" encoding =" UTF-8" ?>
320
316
<container xmlns =" http://symfony.com/schema/dic/services"
321
317
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
@@ -335,7 +331,7 @@ directories don't exist, create them.
335
331
336
332
.. code-block :: php
337
333
338
- // src/AppBundle/Resources/ config/services.php
334
+ // app/ config/services/mailer .php
339
335
use Symfony\Component\DependencyInjection\Definition;
340
336
341
337
$container->setParameter('app.mailer.transport', 'sendmail');
@@ -345,46 +341,42 @@ directories don't exist, create them.
345
341
array('%app.mailer.transport%')
346
342
));
347
343
348
- The definition itself hasn't changed, only its location. Of course the service
349
- container doesn't know about the new resource file. Fortunately, you can
350
- easily import the resource file using the `` imports `` key in the application
351
- configuration.
344
+ The definition itself hasn't changed, only its location. To make the service
345
+ container load the definitions in this resource file, use the `` imports `` key
346
+ in any already loaded resource (e.g. `` app/config/services.yml `` or
347
+ `` app/config/config.yml ``):
352
348
353
349
.. configuration-block ::
354
350
355
351
.. code-block :: yaml
356
352
357
- # app/config/config .yml
353
+ # app/config/services .yml
358
354
imports :
359
- - { resource: '@AppBundle/Resources/config/ services.yml' }
355
+ - { resource: services/mailer .yml }
360
356
361
357
.. code-block :: xml
362
358
363
- <!-- app/config/config .xml -->
359
+ <!-- app/config/services .xml -->
364
360
<?xml version =" 1.0" encoding =" UTF-8" ?>
365
361
<container xmlns =" http://symfony.com/schema/dic/services"
366
362
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
367
363
xsi : schemaLocation =" http://symfony.com/schema/dic/services
368
364
http://symfony.com/schema/dic/services/services-1.0.xsd" >
369
365
370
366
<imports >
371
- <import resource =" @AppBundle/Resources/config/ services.xml" />
367
+ <import resource =" services/mailer .xml" />
372
368
</imports >
373
369
</container >
374
370
375
371
.. code-block :: php
376
372
377
- // app/config/config .php
378
- $loader->import('@AppBundle/Resources/config/ services.php');
373
+ // app/config/services .php
374
+ $loader->import('services/mailer .php');
379
375
380
- .. include :: /components/dependency_injection/_imports-parameters-note.rst.inc
376
+ The ``resource `` location, for files, is either a relative path from the
377
+ current file or an absolute path.
381
378
382
- The ``imports `` directive allows your application to include service container
383
- configuration resources from any other location (most commonly from bundles).
384
- The ``resource `` location, for files, is the absolute path to the resource
385
- file. The special ``@AppBundle `` syntax resolves the directory path
386
- of the AppBundle bundle. This helps you specify the path to the resource
387
- without worrying later if you move the AppBundle to a different directory.
379
+ .. include :: /components/dependency_injection/_imports-parameters-note.rst.inc
388
380
389
381
.. index ::
390
382
single: Service Container; Extension configuration
@@ -394,31 +386,14 @@ without worrying later if you move the AppBundle to a different directory.
394
386
Importing Configuration via Container Extensions
395
387
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
396
388
397
- When developing in Symfony, you'll most commonly use the ``imports `` directive
398
- to import container configuration from the bundles you've created specifically
399
- for your application. Third-party bundle container configuration, including
400
- Symfony core services, are usually loaded using another method that's more
401
- flexible and easy to configure in your application.
402
-
403
- Here's how it works. Internally, each bundle defines its services very much
404
- like you've seen so far. Namely, a bundle uses one or more configuration
405
- resource files (usually XML) to specify the parameters and services for that
406
- bundle. However, instead of importing each of these resources directly from
407
- your application configuration using the ``imports `` directive, you can simply
408
- invoke a *service container extension * inside the bundle that does the work for
409
- you. A service container extension is a PHP class created by the bundle author
410
- to accomplish two things:
411
-
412
- * import all service container resources needed to configure the services for
413
- the bundle;
389
+ Third-party bundle container configuration, including Symfony core services,
390
+ are usually loaded using another method that's more flexible and easy to
391
+ configure in your application.
414
392
415
- * provide semantic, straightforward configuration so that the bundle can
416
- be configured without interacting with the flat parameters of the bundle's
417
- service container configuration.
418
-
419
- In other words, a service container extension configures the services for
420
- a bundle on your behalf. And as you'll see in a moment, the extension provides
421
- a sensible, high-level interface for configuring the bundle.
393
+ Internally, each bundle defines its services like you've seen so far. However,
394
+ these files aren't imported using the ``import `` directive. These bundles use a
395
+ *dependency injection extension * to load the files automatically. The extension
396
+ also allows bundles to provide configuration to dynamically load some services.
422
397
423
398
Take the FrameworkBundle - the core Symfony Framework bundle - as an
424
399
example. The presence of the following code in your application configuration
@@ -430,10 +405,8 @@ invokes the service container extension inside the FrameworkBundle:
430
405
431
406
# app/config/config.yml
432
407
framework :
433
- secret : xxxxxxxxxx
434
- form : true
435
- csrf_protection : true
436
- router : { resource: '%kernel.root_dir%/config/routing.yml' }
408
+ secret : xxxxxxxxxx
409
+ form : true
437
410
# ...
438
411
439
412
.. code-block :: xml
@@ -443,15 +416,13 @@ invokes the service container extension inside the FrameworkBundle:
443
416
<container xmlns =" http://symfony.com/schema/dic/services"
444
417
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
445
418
xmlns : framework =" http://symfony.com/schema/dic/symfony"
446
- xsi : schemaLocation =" http://symfony.com/schema/dic/services
447
- http://symfony.com/schema/dic/services/services-1.0.xsd
448
- http://symfony.com/schema/dic/symfony
449
- http://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
419
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
420
+ http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"
421
+ >
450
422
451
423
<framework : config secret =" xxxxxxxxxx" >
452
424
<framework : form />
453
- <framework : csrf-protection />
454
- <framework : router resource =" %kernel.root_dir%/config/routing.xml" />
425
+
455
426
<!-- ... -->
456
427
</framework >
457
428
</container >
@@ -460,51 +431,31 @@ invokes the service container extension inside the FrameworkBundle:
460
431
461
432
// app/config/config.php
462
433
$container->loadFromExtension('framework', array(
463
- 'secret' => 'xxxxxxxxxx',
464
- 'form' => array(),
465
- 'csrf-protection' => array(),
466
- 'router' => array(
467
- 'resource' => '%kernel.root_dir%/config/routing.php',
468
- ),
434
+ 'secret' => 'xxxxxxxxxx',
435
+ 'form' => array(),
469
436
470
437
// ...
471
438
));
472
439
473
- When the configuration is parsed, the container looks for an extension that
474
- can handle the ``framework `` configuration directive. The extension in question,
475
- which lives in the FrameworkBundle, is invoked and the service configuration
476
- for the FrameworkBundle is loaded. If you remove the ``framework `` key
477
- from your application configuration file entirely, the core Symfony services
478
- won't be loaded. The point is that you're in control: the Symfony Framework
479
- doesn't contain any magic or perform any actions that you don't have control
480
- over.
481
-
482
- Of course you can do much more than simply "activate" the service container
483
- extension of the FrameworkBundle. Each extension allows you to easily
484
- customize the bundle, without worrying about how the internal services are
485
- defined.
486
-
487
- In this case, the extension allows you to customize the ``error_handler ``,
488
- ``csrf_protection ``, ``router `` configuration and much more. Internally,
489
- the FrameworkBundle uses the options specified here to define and configure
490
- the services specific to it. The bundle takes care of creating all the necessary
491
- ``parameters `` and ``services `` for the service container, while still allowing
492
- much of the configuration to be easily customized. As a bonus, most
493
- service container extensions are also smart enough to perform validation -
494
- notifying you of options that are missing or the wrong data type.
440
+ When the resources are parsed, the container looks for an extension that
441
+ can handle the ``framework `` directive. The extension in question, which lives
442
+ in the FrameworkBundle, is invoked and the service configuration for the
443
+ FrameworkBundle is loaded.
444
+
445
+ The settings under the ``framework `` directive (e.g. ``form: true ``) indicates
446
+ that the extension should load all services related to the Form component. If
447
+ form was disabled, these services wouldn't be loaded and Form integration would
448
+ not be available.
495
449
496
450
When installing or configuring a bundle, see the bundle's documentation for
497
451
how the services for the bundle should be installed and configured. The options
498
452
available for the core bundles can be found inside the :doc: `Reference Guide </reference/index >`.
499
453
500
- .. note ::
501
-
502
- Natively, the service container only recognizes the ``parameters ``,
503
- ``services ``, and ``imports `` directives. Any other directives
504
- are handled by a service container extension.
454
+ .. seealso ::
505
455
506
- If you want to expose user friendly configuration in your own bundles, read the
507
- ":doc: `/cookbook/bundles/extension `" cookbook recipe.
456
+ If you want to use dependency injection extensions in your own shared
457
+ bundles and provide user friendly configuration, take a look at the
458
+ ":doc: `/cookbook/bundles/extension `" cookbook recipe.
508
459
509
460
.. index ::
510
461
single: Service Container; Referencing services
0 commit comments