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