@@ -32,15 +32,16 @@ service's class or interface name. Want to :doc:`log </logging>` something? No p
32
32
namespace App\Controller;
33
33
34
34
use Psr\Log\LoggerInterface;
35
+ use Symfony\Component\HttpFoundation\Response;
35
36
36
37
class ProductController
37
38
{
38
39
/**
39
40
* @Route("/products")
40
41
*/
41
- public function list(LoggerInterface $logger)
42
+ public function list(LoggerInterface $logger): Response
42
43
{
43
- $logger->info('Look! I just used a service');
44
+ $logger->info('Look, I just used a service! ');
44
45
45
46
// ...
46
47
}
@@ -101,7 +102,7 @@ it can't be re-used. Instead, you decide to create a new class::
101
102
102
103
class MessageGenerator
103
104
{
104
- public function getHappyMessage()
105
+ public function getHappyMessage(): string
105
106
{
106
107
$messages = [
107
108
'You did it! You updated the system! Amazing!',
@@ -118,9 +119,14 @@ it can't be re-used. Instead, you decide to create a new class::
118
119
Congratulations! You've created your first service class! You can use it immediately
119
120
inside your controller::
120
121
122
+ // src/Controller/ProductController.php
121
123
use App\Service\MessageGenerator;
124
+ use Symfony\Component\HttpFoundation\Response;
122
125
123
- public function new(MessageGenerator $messageGenerator)
126
+ /**
127
+ * @Route("/products/new")
128
+ */
129
+ public function new(MessageGenerator $messageGenerator): Response
124
130
{
125
131
// thanks to the type-hint, the container will instantiate a
126
132
// new MessageGenerator and pass it to you!
@@ -241,7 +247,7 @@ and use it later::
241
247
$this->logger = $logger;
242
248
}
243
249
244
- public function getHappyMessage()
250
+ public function getHappyMessage(): string
245
251
{
246
252
$this->logger->info('About to find a happy message!');
247
253
// ...
@@ -291,8 +297,8 @@ Handling Multiple Services
291
297
Suppose you also want to email a site administrator each time a site update is
292
298
made. To do that, you create a new class::
293
299
294
- // src/Updates /SiteUpdateManager.php
295
- namespace App\Updates ;
300
+ // src/Service /SiteUpdateManager.php
301
+ namespace App\Service ;
296
302
297
303
use App\Service\MessageGenerator;
298
304
use Symfony\Component\Mailer\MailerInterface;
@@ -309,7 +315,7 @@ made. To do that, you create a new class::
309
315
$this->mailer = $mailer;
310
316
}
311
317
312
- public function notifyOfSiteUpdate()
318
+ public function notifyOfSiteUpdate(): bool
313
319
{
314
320
$happyMessage = $this->messageGenerator->getHappyMessage();
315
321
@@ -322,6 +328,8 @@ made. To do that, you create a new class::
322
328
$this->mailer->send($email);
323
329
324
330
// ...
331
+
332
+ return true;
325
333
}
326
334
}
327
335
@@ -334,7 +342,7 @@ you can type-hint the new ``SiteUpdateManager`` class and use it::
334
342
namespace App\Controller;
335
343
336
344
// ...
337
- use App\Updates \SiteUpdateManager;
345
+ use App\Service \SiteUpdateManager;
338
346
339
347
public function new(SiteUpdateManager $siteUpdateManager)
340
348
{
@@ -361,29 +369,29 @@ example, suppose you want to make the admin email configurable:
361
369
362
370
.. code-block :: diff
363
371
364
- // src/Updates /SiteUpdateManager.php
372
+ // src/Service /SiteUpdateManager.php
365
373
// ...
366
374
367
375
class SiteUpdateManager
368
376
{
369
377
// ...
370
378
+ private $adminEmail;
371
379
372
- - public function __construct(MessageGenerator $messageGenerator, \Swift_Mailer $mailer)
373
- + public function __construct(MessageGenerator $messageGenerator, \Swift_Mailer $mailer, $adminEmail)
380
+ - public function __construct(MessageGenerator $messageGenerator, MailerInterface $mailer)
381
+ + public function __construct(MessageGenerator $messageGenerator, MailerInterface $mailer, $adminEmail)
374
382
{
375
383
// ...
376
384
+ $this->adminEmail = $adminEmail;
377
385
}
378
386
379
- public function notifyOfSiteUpdate()
387
+ public function notifyOfSiteUpdate(): bool
380
388
{
381
389
// ...
382
390
383
- $message = \Swift_Message::newInstance( )
391
+ $email = (new Email() )
384
392
// ...
385
- - ->setTo ('manager@example.com')
386
- + ->setTo ($this->adminEmail)
393
+ - ->to ('manager@example.com')
394
+ + ->to ($this->adminEmail)
387
395
// ...
388
396
;
389
397
// ...
@@ -392,7 +400,7 @@ example, suppose you want to make the admin email configurable:
392
400
393
401
If you make this change and refresh, you'll see an error:
394
402
395
- Cannot autowire service "App\U pdates \S iteUpdateManager": argument "$adminEmail"
403
+ Cannot autowire service "App\S ervice \S iteUpdateManager": argument "$adminEmail"
396
404
of method "__construct()" must have a type-hint or be given a value explicitly.
397
405
398
406
That makes sense! There is no way that the container knows what value you want to
@@ -412,7 +420,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
412
420
exclude : ' ../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
413
421
414
422
# explicitly configure the service
415
- App\Updates \SiteUpdateManager :
423
+ App\Service \SiteUpdateManager :
416
424
arguments :
417
425
$adminEmail : ' manager@example.com'
418
426
@@ -436,7 +444,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
436
444
/>
437
445
438
446
<!-- Explicitly configure the service -->
439
- <service id =" App\Updates \SiteUpdateManager" >
447
+ <service id =" App\Service \SiteUpdateManager" >
440
448
<argument key =" $adminEmail" >manager@example.com</argument >
441
449
</service >
442
450
</services >
@@ -447,7 +455,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
447
455
// config/services.php
448
456
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
449
457
450
- use App\Updates \SiteUpdateManager;
458
+ use App\Service \SiteUpdateManager;
451
459
452
460
return function(ContainerConfigurator $configurator) {
453
461
// ...
@@ -877,7 +885,7 @@ But, if you *do* need to make a service public, override the ``public`` setting:
877
885
# ... same code as before
878
886
879
887
# explicitly configure the service
880
- Acme \PublicService :
888
+ App\Service \PublicService :
881
889
public : true
882
890
883
891
.. code-block :: xml
@@ -893,7 +901,7 @@ But, if you *do* need to make a service public, override the ``public`` setting:
893
901
<!-- ... same code as before -->
894
902
895
903
<!-- Explicitly configure the service -->
896
- <service id =" Acme \PublicService" public =" true" ></service >
904
+ <service id =" App\Service \PublicService" public =" true" ></service >
897
905
</services >
898
906
</container >
899
907
@@ -902,13 +910,13 @@ But, if you *do* need to make a service public, override the ``public`` setting:
902
910
// config/services.php
903
911
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
904
912
905
- use Acme \PublicService;
913
+ use App\Service \PublicService;
906
914
907
915
return function(ContainerConfigurator $configurator) {
908
916
// ... same as code before
909
917
910
918
// explicitly configure the service
911
- $services->set(PublicService::class)
919
+ $services->set(Service\ PublicService::class)
912
920
->public()
913
921
;
914
922
};
@@ -1087,7 +1095,7 @@ admin email. In this case, each needs to have a unique service id:
1087
1095
1088
1096
# this is the service's id
1089
1097
site_update_manager.superadmin :
1090
- class : App\Updates \SiteUpdateManager
1098
+ class : App\Service \SiteUpdateManager
1091
1099
# you CAN still use autowiring: we just want to show what it looks like without
1092
1100
autowire : false
1093
1101
# manually wire all arguments
@@ -1097,7 +1105,7 @@ admin email. In this case, each needs to have a unique service id:
1097
1105
- ' superadmin@example.com'
1098
1106
1099
1107
site_update_manager.normal_users :
1100
- class : App\Updates \SiteUpdateManager
1108
+ class : App\Service \SiteUpdateManager
1101
1109
autowire : false
1102
1110
arguments :
1103
1111
- ' @App\Service\MessageGenerator'
@@ -1106,7 +1114,7 @@ admin email. In this case, each needs to have a unique service id:
1106
1114
1107
1115
# Create an alias, so that - by default - if you type-hint SiteUpdateManager,
1108
1116
# the site_update_manager.superadmin will be used
1109
- App\Updates \SiteUpdateManager : ' @site_update_manager.superadmin'
1117
+ App\Service \SiteUpdateManager : ' @site_update_manager.superadmin'
1110
1118
1111
1119
.. code-block :: xml
1112
1120
@@ -1120,19 +1128,19 @@ admin email. In this case, each needs to have a unique service id:
1120
1128
<services >
1121
1129
<!-- ... -->
1122
1130
1123
- <service id =" site_update_manager.superadmin" class =" App\Updates \SiteUpdateManager" autowire =" false" >
1131
+ <service id =" site_update_manager.superadmin" class =" App\Service \SiteUpdateManager" autowire =" false" >
1124
1132
<argument type =" service" id =" App\Service\MessageGenerator" />
1125
1133
<argument type =" service" id =" mailer" />
1126
1134
<argument >superadmin@example.com</argument >
1127
1135
</service >
1128
1136
1129
- <service id =" site_update_manager.normal_users" class =" App\Updates \SiteUpdateManager" autowire =" false" >
1137
+ <service id =" site_update_manager.normal_users" class =" App\Service \SiteUpdateManager" autowire =" false" >
1130
1138
<argument type =" service" id =" App\Service\MessageGenerator" />
1131
1139
<argument type =" service" id =" mailer" />
1132
1140
<argument >contact@example.com</argument >
1133
1141
</service >
1134
1142
1135
- <service id =" App\Updates \SiteUpdateManager" alias =" site_update_manager.superadmin" />
1143
+ <service id =" App\Service \SiteUpdateManager" alias =" site_update_manager.superadmin" />
1136
1144
</services >
1137
1145
</container >
1138
1146
@@ -1142,7 +1150,7 @@ admin email. In this case, each needs to have a unique service id:
1142
1150
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1143
1151
1144
1152
use App\Service\MessageGenerator;
1145
- use App\Updates \SiteUpdateManager;
1153
+ use App\Service \SiteUpdateManager;
1146
1154
1147
1155
return function(ContainerConfigurator $configurator) {
1148
1156
// ...
0 commit comments