@@ -10,15 +10,10 @@ send emails while another object might help you save things to the database.
10
10
Almost *everything * that your app "does" is actually done by one of these objects.
11
11
And each time you install a new bundle, you get access to even more!
12
12
13
- In Symfony, these useful objects are called **services ** and each service lives inside
14
- a very special object called the **service container **. If you have the service container,
15
- then you can fetch a service by using that service's id::
16
-
17
- $logger = $container->get('logger');
18
- $entityManager = $container->get('doctrine.orm.entity_manager');
19
-
20
- The container allows you to centralize the way objects are constructed. It makes
21
- your life easier, promotes a strong architecture and is super fast!
13
+ In Symfony, these useful objects are called **services ** and each service lives
14
+ inside a very special object called the **service container **. The container
15
+ allows you to centralize the way objects are constructed. It makes your life
16
+ easier, promotes a strong architecture and is super fast!
22
17
23
18
Fetching and using Services
24
19
---------------------------
@@ -36,7 +31,7 @@ service's class or interface name. Want to :doc:`log </logging>` something? No p
36
31
/**
37
32
* @Route("/products")
38
33
*/
39
- public function listAction (LoggerInterface $logger)
34
+ public function list (LoggerInterface $logger)
40
35
{
41
36
$logger->info('Look! I just used a service');
42
37
@@ -83,7 +78,7 @@ You can also use the unique "Service ID" to access a service directly::
83
78
/**
84
79
* @Route("/products")
85
80
*/
86
- public function listAction ()
81
+ public function list ()
87
82
{
88
83
$logger = $this->container->get('logger');
89
84
$logger->info('Look! I just used a service');
@@ -146,7 +141,7 @@ inside your controller::
146
141
147
142
use App\Service\MessageGenerator;
148
143
149
- public function newAction (MessageGenerator $messageGenerator)
144
+ public function new (MessageGenerator $messageGenerator)
150
145
{
151
146
// thanks to the type-hint, the container will instantiate a
152
147
// new MessageGenerator and pass it to you!
@@ -167,9 +162,7 @@ each time you ask for it.
167
162
168
163
.. sidebar :: Automatic Service Loading in services.yaml
169
164
170
- The documentation assumes you're using
171
- `Symfony Standard Edition (version 3.3) services.yaml `_ configuration. The most
172
- important part is this:
165
+ The documentation assumes you're using the following service configuration:
173
166
174
167
.. configuration-block ::
175
168
@@ -185,10 +178,10 @@ each time you ask for it.
185
178
186
179
# makes classes in src/ available to be used as services
187
180
App\ :
188
- resource : ' ../../ src/*'
181
+ resource : ' ../src/*'
189
182
# you can exclude directories or files
190
183
# but if a service is unused, it's removed anyway
191
- exclude : ' ../../ src/{Entity,Repository}'
184
+ exclude : ' ../src/{Entity,Repository}'
192
185
193
186
.. code-block :: xml
194
187
@@ -204,7 +197,7 @@ each time you ask for it.
204
197
<defaults autowire =" true" autoconfigure =" true" public =" false" />
205
198
206
199
<!-- Load services from whatever directories you want (you can update this!) -->
207
- <prototype namespace =" App\" resource =" ../../ src/*" exclude =" ../ ../src/{Entity,Repository}" />
200
+ <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{Entity,Repository}" />
208
201
</services >
209
202
</container >
210
203
@@ -223,7 +216,7 @@ each time you ask for it.
223
216
;
224
217
225
218
// $this is a reference to the current loader
226
- $this->registerClasses($definition, 'App\\', '../../ src/*', '../ ../src/{Entity,Repository}');
219
+ $this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Repository}');
227
220
228
221
.. tip ::
229
222
@@ -237,15 +230,16 @@ each time you ask for it.
237
230
If you'd prefer to manually wire your service, that's totally possible: see
238
231
:ref: `services-explicitly-configure-wire-services `.
239
232
240
- You can also fetch a service directly from the container via its "id", which will
241
- be its class name in this case::
233
+ If the :ref: `service is public <container-public >`, you can also fetch it
234
+ directly from the container via its "id". However, this practice is discouraged
235
+ and you should instead inject services via constructors::
242
236
243
237
use App\Service\MessageGenerator;
244
238
245
239
// accessing services like this only works if you extend Controller
246
240
class ProductController extends Controller
247
241
{
248
- public function newAction ()
242
+ public function new ()
249
243
{
250
244
// only works if your service is public
251
245
$messageGenerator = $this->get(MessageGenerator::class);
@@ -256,8 +250,6 @@ be its class name in this case::
256
250
}
257
251
}
258
252
259
- However, this only works if you make your service :ref: `public <container-public >`.
260
-
261
253
.. _services-constructor-injection :
262
254
263
255
Injecting Services/Config into a Service
@@ -368,7 +360,7 @@ you can use the service immediately::
368
360
369
361
use App\Updates\SiteUpdateManager;
370
362
371
- public function newAction (SiteUpdateManager $siteUpdateManager)
363
+ public function new (SiteUpdateManager $siteUpdateManager)
372
364
{
373
365
// ...
374
366
@@ -438,8 +430,8 @@ pass here. No problem! In your configuration, you can explicitly set this argume
438
430
439
431
# same as before
440
432
App\ :
441
- resource : ' ../../ src/*'
442
- exclude : ' ../../ src/{Entity,Repository}'
433
+ resource : ' ../src/*'
434
+ exclude : ' ../src/{Entity,Repository}'
443
435
444
436
# explicitly configure the service
445
437
App\Updates\SiteUpdateManager :
@@ -459,7 +451,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
459
451
<!-- ... -->
460
452
461
453
<!-- Same as before -->
462
- <prototype namespace =" App\" resource =" ../../ src/*" exclude =" ../ ../src/{Entity,Repository}" />
454
+ <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{Entity,Repository}" />
463
455
464
456
<!-- Explicitly configure the service -->
465
457
<service id =" App\Updates\SiteUpdateManager" >
@@ -483,7 +475,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
483
475
->setPublic(false)
484
476
;
485
477
486
- $this->registerClasses($definition, 'App\\', '../../ src/*', '../ ../src/{Entity,Repository}');
478
+ $this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Repository}');
487
479
488
480
// Explicitly configure the service
489
481
$container->getDefinition(SiteUpdateManager::class)
@@ -553,9 +545,9 @@ and reference it with the ``%parameter_name%`` syntax:
553
545
// ...
554
546
->setArgument('$adminEmail', '%admin_email%');
555
547
556
- Actually, once you define a parameter, it can be referenced via the `` %parameter_name% ``
557
- syntax in *any * other service configuration file - like `` config.yml `` . Many parameters
558
- are defined in a :ref: ` parameters.yml file < config-parameters-yml >` .
548
+ Actually, once you define a parameter, it can be referenced via the
549
+ `` %parameter_name% `` syntax in *any * other configuration file. Many parameters
550
+ are defined in the `` config/services.yaml `` file.
559
551
560
552
You can then fetch the parameter in the service::
561
553
@@ -573,11 +565,11 @@ You can then fetch the parameter in the service::
573
565
574
566
You can also fetch parameters directly from the container::
575
567
576
- public function newAction ()
568
+ public function new ()
577
569
{
578
570
// ...
579
571
580
- // this ONLY works if you extend Controller
572
+ // this ONLY works if you extend the base Controller
581
573
$adminEmail = $this->container->getParameter('admin_email');
582
574
583
575
// or a shorter way!
@@ -741,7 +733,7 @@ as a service, and :doc:`tag </service_container/tags>` it with ``twig.extension`
741
733
->addTag('twig.extension');
742
734
743
735
But, with ``autoconfigure: true ``, you don't need the tag. In fact, if you're using
744
- the :ref: `Symfony Standard Edition services.yaml config <service-container-services-load-example >`,
736
+ the :ref: `default services.yaml config <service-container-services-load-example >`,
745
737
you don't need to do *anything *: the service will be automatically loaded. Then,
746
738
``autoconfigure `` will add the ``twig.extension `` tag *for * you, because your class
747
739
implements ``Twig_ExtensionInterface ``. And thanks to ``autowire ``, you can even add
@@ -789,15 +781,15 @@ from the container::
789
781
790
782
use App\Service\MessageGenerator;
791
783
792
- public function newAction (MessageGenerator $messageGenerator)
784
+ public function new (MessageGenerator $messageGenerator)
793
785
{
794
786
// type-hinting it as an argument DOES work
795
787
796
788
// but accessing it directly from the container does NOT Work
797
789
$this->container->get(MessageGenerator::class);
798
790
}
799
791
800
- Usually, this is ok : there are better ways to access a service. But, if you *do *
792
+ Usually, this is OK : there are better ways to access a service. But, if you *do *
801
793
need to make your service public, just override this setting:
802
794
803
795
.. configuration-block ::
@@ -848,13 +840,13 @@ key. For example, the default Symfony configuration contains this:
848
840
# the namespace prefix for classes (must end in \)
849
841
App\ :
850
842
# create services for all the classes found in this directory...
851
- resource : ' ../../ src/*'
843
+ resource : ' ../src/*'
852
844
# ...except for the classes located in these directories
853
- exclude : ' ../../ src/{Entity,Repository}'
845
+ exclude : ' ../src/{Entity,Repository}'
854
846
855
847
# these were imported above, but we want to add some extra config
856
848
App\Controller\ :
857
- resource : ' ../../ src/Controller'
849
+ resource : ' ../src/Controller'
858
850
# apply some configuration to these services
859
851
public : true
860
852
tags : ['controller.service_arguments']
@@ -871,9 +863,9 @@ key. For example, the default Symfony configuration contains this:
871
863
<services >
872
864
<!-- ... -->
873
865
874
- <prototype namespace =" App\" resource =" ../../ src/*" exclude =" ../ ../src/{Entity,Repository}" />
866
+ <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{Entity,Repository}" />
875
867
876
- <prototype namespace =" App\Controller\" resource =" ../../ src/Controller" public =" true" >
868
+ <prototype namespace =" App\Controller\" resource =" ../src/Controller" public =" true" >
877
869
<tag name =" controller.service_arguments" />
878
870
</prototype >
879
871
</services >
@@ -893,7 +885,7 @@ key. For example, the default Symfony configuration contains this:
893
885
->setPublic(false)
894
886
;
895
887
896
- $this->registerClasses($definition, 'App\\', '../../ src/*', '../ ../src/{Entity,Repository}');
888
+ $this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Repository}');
897
889
898
890
// Changes default config
899
891
$definition
@@ -902,7 +894,7 @@ key. For example, the default Symfony configuration contains this:
902
894
;
903
895
904
896
// $this is a reference to the current loader
905
- $this->registerClasses($definition, 'App\\Controller\\', '../../ src/Controller/*');
897
+ $this->registerClasses($definition, 'App\\Controller\\', '../src/Controller/*');
906
898
907
899
.. tip ::
908
900
0 commit comments