@@ -32,15 +32,16 @@ service's class or interface name. Want to :doc:`log </logging>` something? No p
3232 namespace App\Controller;
3333
3434 use Psr\Log\LoggerInterface;
35+ use Symfony\Component\HttpFoundation\Response;
3536
3637 class ProductController
3738 {
3839 /**
3940 * @Route("/products")
4041 */
41- public function list(LoggerInterface $logger)
42+ public function list(LoggerInterface $logger): Response
4243 {
43- $logger->info('Look! I just used a service');
44+ $logger->info('Look, I just used a service! ');
4445
4546 // ...
4647 }
@@ -101,7 +102,7 @@ it can't be re-used. Instead, you decide to create a new class::
101102
102103 class MessageGenerator
103104 {
104- public function getHappyMessage()
105+ public function getHappyMessage(): string
105106 {
106107 $messages = [
107108 '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::
118119Congratulations! You've created your first service class! You can use it immediately
119120inside your controller::
120121
122+ // src/Controller/ProductController.php
121123 use App\Service\MessageGenerator;
124+ use Symfony\Component\HttpFoundation\Response;
122125
123- public function new(MessageGenerator $messageGenerator)
126+ /**
127+ * @Route("/products/new")
128+ */
129+ public function new(MessageGenerator $messageGenerator): Response
124130 {
125131 // thanks to the type-hint, the container will instantiate a
126132 // new MessageGenerator and pass it to you!
@@ -241,7 +247,7 @@ and use it later::
241247 $this->logger = $logger;
242248 }
243249
244- public function getHappyMessage()
250+ public function getHappyMessage(): string
245251 {
246252 $this->logger->info('About to find a happy message!');
247253 // ...
@@ -291,8 +297,8 @@ Handling Multiple Services
291297Suppose you also want to email a site administrator each time a site update is
292298made. To do that, you create a new class::
293299
294- // src/Updates /SiteUpdateManager.php
295- namespace App\Updates ;
300+ // src/Service /SiteUpdateManager.php
301+ namespace App\Service ;
296302
297303 use App\Service\MessageGenerator;
298304 use Symfony\Component\Mailer\MailerInterface;
@@ -309,7 +315,7 @@ made. To do that, you create a new class::
309315 $this->mailer = $mailer;
310316 }
311317
312- public function notifyOfSiteUpdate()
318+ public function notifyOfSiteUpdate(): bool
313319 {
314320 $happyMessage = $this->messageGenerator->getHappyMessage();
315321
@@ -322,6 +328,8 @@ made. To do that, you create a new class::
322328 $this->mailer->send($email);
323329
324330 // ...
331+
332+ return true;
325333 }
326334 }
327335
@@ -334,7 +342,7 @@ you can type-hint the new ``SiteUpdateManager`` class and use it::
334342 namespace App\Controller;
335343
336344 // ...
337- use App\Updates \SiteUpdateManager;
345+ use App\Service \SiteUpdateManager;
338346
339347 public function new(SiteUpdateManager $siteUpdateManager)
340348 {
@@ -361,29 +369,29 @@ example, suppose you want to make the admin email configurable:
361369
362370.. code-block :: diff
363371
364- // src/Updates /SiteUpdateManager.php
372+ // src/Service /SiteUpdateManager.php
365373 // ...
366374
367375 class SiteUpdateManager
368376 {
369377 // ...
370378 + private $adminEmail;
371379
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)
374382 {
375383 // ...
376384 + $this->adminEmail = $adminEmail;
377385 }
378386
379- public function notifyOfSiteUpdate()
387+ public function notifyOfSiteUpdate(): bool
380388 {
381389 // ...
382390
383- $message = \Swift_Message::newInstance( )
391+ $email = (new Email() )
384392 // ...
385- - ->setTo ('manager@example.com')
386- + ->setTo ($this->adminEmail)
393+ - ->to ('manager@example.com')
394+ + ->to ($this->adminEmail)
387395 // ...
388396 ;
389397 // ...
@@ -392,7 +400,7 @@ example, suppose you want to make the admin email configurable:
392400
393401 If you make this change and refresh, you'll see an error:
394402
395- Cannot autowire service "App\U pdates \S iteUpdateManager": argument "$adminEmail"
403+ Cannot autowire service "App\S ervice \S iteUpdateManager": argument "$adminEmail"
396404 of method "__construct()" must have a type-hint or be given a value explicitly.
397405
398406That 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
412420 exclude : ' ../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
413421
414422 # explicitly configure the service
415- App\Updates \SiteUpdateManager :
423+ App\Service \SiteUpdateManager :
416424 arguments :
417425 $adminEmail : ' manager@example.com'
418426
@@ -436,7 +444,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
436444 />
437445
438446 <!-- Explicitly configure the service -->
439- <service id =" App\Updates \SiteUpdateManager" >
447+ <service id =" App\Service \SiteUpdateManager" >
440448 <argument key =" $adminEmail" >manager@example.com</argument >
441449 </service >
442450 </services >
@@ -447,7 +455,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
447455 // config/services.php
448456 namespace Symfony\Component\DependencyInjection\Loader\Configurator;
449457
450- use App\Updates \SiteUpdateManager;
458+ use App\Service \SiteUpdateManager;
451459
452460 return function(ContainerConfigurator $configurator) {
453461 // ...
@@ -877,7 +885,7 @@ But, if you *do* need to make a service public, override the ``public`` setting:
877885 # ... same code as before
878886
879887 # explicitly configure the service
880- Acme \PublicService :
888+ App\Service \PublicService :
881889 public : true
882890
883891 .. code-block :: xml
@@ -893,7 +901,7 @@ But, if you *do* need to make a service public, override the ``public`` setting:
893901 <!-- ... same code as before -->
894902
895903 <!-- Explicitly configure the service -->
896- <service id =" Acme \PublicService" public =" true" ></service >
904+ <service id =" App\Service \PublicService" public =" true" ></service >
897905 </services >
898906 </container >
899907
@@ -902,13 +910,13 @@ But, if you *do* need to make a service public, override the ``public`` setting:
902910 // config/services.php
903911 namespace Symfony\Component\DependencyInjection\Loader\Configurator;
904912
905- use Acme \PublicService;
913+ use App\Service \PublicService;
906914
907915 return function(ContainerConfigurator $configurator) {
908916 // ... same as code before
909917
910918 // explicitly configure the service
911- $services->set(PublicService::class)
919+ $services->set(Service\ PublicService::class)
912920 ->public()
913921 ;
914922 };
@@ -1087,7 +1095,7 @@ admin email. In this case, each needs to have a unique service id:
10871095
10881096 # this is the service's id
10891097 site_update_manager.superadmin :
1090- class : App\Updates \SiteUpdateManager
1098+ class : App\Service \SiteUpdateManager
10911099 # you CAN still use autowiring: we just want to show what it looks like without
10921100 autowire : false
10931101 # manually wire all arguments
@@ -1097,7 +1105,7 @@ admin email. In this case, each needs to have a unique service id:
10971105 - ' superadmin@example.com'
10981106
10991107 site_update_manager.normal_users :
1100- class : App\Updates \SiteUpdateManager
1108+ class : App\Service \SiteUpdateManager
11011109 autowire : false
11021110 arguments :
11031111 - ' @App\Service\MessageGenerator'
@@ -1106,7 +1114,7 @@ admin email. In this case, each needs to have a unique service id:
11061114
11071115 # Create an alias, so that - by default - if you type-hint SiteUpdateManager,
11081116 # the site_update_manager.superadmin will be used
1109- App\Updates \SiteUpdateManager : ' @site_update_manager.superadmin'
1117+ App\Service \SiteUpdateManager : ' @site_update_manager.superadmin'
11101118
11111119 .. code-block :: xml
11121120
@@ -1120,19 +1128,19 @@ admin email. In this case, each needs to have a unique service id:
11201128 <services >
11211129 <!-- ... -->
11221130
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" >
11241132 <argument type =" service" id =" App\Service\MessageGenerator" />
11251133 <argument type =" service" id =" mailer" />
11261134 <argument >superadmin@example.com</argument >
11271135 </service >
11281136
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" >
11301138 <argument type =" service" id =" App\Service\MessageGenerator" />
11311139 <argument type =" service" id =" mailer" />
11321140 <argument >contact@example.com</argument >
11331141 </service >
11341142
1135- <service id =" App\Updates \SiteUpdateManager" alias =" site_update_manager.superadmin" />
1143+ <service id =" App\Service \SiteUpdateManager" alias =" site_update_manager.superadmin" />
11361144 </services >
11371145 </container >
11381146
@@ -1142,7 +1150,7 @@ admin email. In this case, each needs to have a unique service id:
11421150 namespace Symfony\Component\DependencyInjection\Loader\Configurator;
11431151
11441152 use App\Service\MessageGenerator;
1145- use App\Updates \SiteUpdateManager;
1153+ use App\Service \SiteUpdateManager;
11461154
11471155 return function(ContainerConfigurator $configurator) {
11481156 // ...
0 commit comments