@@ -382,6 +382,127 @@ type-hints by running:
382
382
383
383
[...]
384
384
385
+ In addition to injecting services, you can also pass scalar values and collections
386
+ as arguments of other services:
387
+
388
+ .. configuration-block ::
389
+
390
+ .. code-block :: yaml
391
+
392
+ # config/services.yaml
393
+ services :
394
+ App\Service\SomeService :
395
+ arguments :
396
+ # string, numeric and boolean arguments can be passed "as is"
397
+ - ' Foo'
398
+ - true
399
+ - 7
400
+ - 3.14
401
+
402
+ # constants can be built-in, user-defined, or Enums
403
+ - !php/const E_ALL
404
+ - !php/const PDO::FETCH_NUM
405
+ - !php/const Symfony\Component\HttpKernel\Kernel::VERSION
406
+ - !php/const App\Config\SomeEnum::SomeCase
407
+
408
+ # when not using autowiring, you can pass service arguments explicitly
409
+ - ' @some-service-id' # the leading '@' tells this is a service ID, not a string
410
+ - ' @?some-service-id' # using '?' means to pass null if service doesn't exist
411
+
412
+ # binary contents are passed encoded as base64 strings
413
+ - !!binary VGhpcyBpcyBhIEJlbGwgY2hhciAH
414
+
415
+ # collections (arrays) can include any type of argument
416
+ -
417
+ first : !php/const true
418
+ second : ' Foo'
419
+
420
+ .. code-block :: xml
421
+
422
+ <!-- config/services.xml -->
423
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
424
+ <container xmlns =" http://symfony.com/schema/dic/services"
425
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
426
+ xmlns : framework =" http://symfony.com/schema/dic/symfony"
427
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
428
+ https://symfony.com/schema/dic/services/services-1.0.xsd
429
+ http://symfony.com/schema/dic/symfony
430
+ https://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
431
+
432
+ <services >
433
+ <service id =" App\Service\SomeService" >
434
+ <!-- arguments without a type can be strings or numbers -->
435
+ <argument >Foo</argument >
436
+ <argument >7</argument >
437
+ <argument >3.14</argument >
438
+ <!-- explicitly declare a string argument -->
439
+ <argument type =" string" >Foo</argument >
440
+ <!-- booleans are passed as constants -->
441
+ <argument type =" constant" >true</argument >
442
+
443
+ <!-- constants can be built-in, user-defined, or Enums -->
444
+ <argument type =" constant" >E_ALL</argument >
445
+ <argument type =" constant" >PDO::FETCH_NUM</argument >
446
+ <argument type =" constant" >Symfony\Component\HttpKernel\Kernel::VERSION</argument >
447
+ <argument type =" constant" >App\Config\SomeEnum::SomeCase</argument >
448
+
449
+ <!-- when not using autowiring, you can pass service arguments explicitly -->
450
+ <argument type =" service"
451
+ id =" some-service-id"
452
+ on-invalid =" dependency_injection-ignore" />
453
+
454
+ <!-- binary contents are passed encoded as base64 strings -->
455
+ <argument type =" binary" >VGhpcyBpcyBhIEJlbGwgY2hhciAH</argument >
456
+
457
+ <!-- collections (arrays) can include any type of argument -->
458
+ <argument type =" collection" >
459
+ <argument key =" first" type =" constant" >true</argument >
460
+ <argument key =" second" type =" string" >Foo</argument >
461
+ </argument >
462
+ </service >
463
+
464
+ <!-- ... -->
465
+ </services >
466
+ </container >
467
+
468
+ .. code-block :: php
469
+
470
+ // config/services.php
471
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
472
+
473
+ use Symfony\Component\DependencyInjection\ContainerInterface;
474
+ use Symfony\Component\DependencyInjection\Reference;
475
+
476
+ return static function (ContainerConfigurator $container) {
477
+ $services = $container->services();
478
+
479
+ $services->set(App\Service\SomeService::class)
480
+ // string, numeric and boolean arguments can be passed "as is"
481
+ ->arg(0, 'Foo')
482
+ ->arg(1, true)
483
+ ->arg(2, 7)
484
+ ->arg(3, 3.14)
485
+
486
+ // constants: built-in, user-defined, or Enums
487
+ ->arg(4, E_ALL)
488
+ ->arg(5, \PDO::FETCH_NUM)
489
+ ->arg(6, Symfony\Component\HttpKernel\Kernel::VERSION)
490
+ ->arg(7, App\Config\SomeEnum::SomeCase)
491
+
492
+ // when not using autowiring, you can pass service arguments explicitly
493
+ ->arg(8, service('some-service-id')) # fails if service doesn't exist
494
+ # passes null if service doesn't exist
495
+ ->arg(9, new Reference('some-service-id', Reference::IGNORE_ON_INVALID_REFERENCE))
496
+
497
+ // collection with mixed argument types
498
+ ->arg(10, [
499
+ 'first' => true,
500
+ 'second' => 'Foo',
501
+ ]);
502
+
503
+ // ...
504
+ };
505
+
385
506
Handling Multiple Services
386
507
~~~~~~~~~~~~~~~~~~~~~~~~~~
387
508
0 commit comments