@@ -10,15 +10,10 @@ send emails while another object might help you save things to the database.
1010Almost *everything * that your app "does" is actually done by one of these objects.
1111And each time you install a new bundle, you get access to even more!
1212
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!
2217
2318Fetching and using Services
2419---------------------------
@@ -36,7 +31,7 @@ service's class or interface name. Want to :doc:`log </logging>` something? No p
3631 /**
3732 * @Route("/products")
3833 */
39- public function listAction (LoggerInterface $logger)
34+ public function list (LoggerInterface $logger)
4035 {
4136 $logger->info('Look! I just used a service');
4237
@@ -83,7 +78,7 @@ You can also use the unique "Service ID" to access a service directly::
8378 /**
8479 * @Route("/products")
8580 */
86- public function listAction ()
81+ public function list ()
8782 {
8883 $logger = $this->container->get('logger');
8984 $logger->info('Look! I just used a service');
@@ -146,7 +141,7 @@ inside your controller::
146141
147142 use App\Service\MessageGenerator;
148143
149- public function newAction (MessageGenerator $messageGenerator)
144+ public function new (MessageGenerator $messageGenerator)
150145 {
151146 // thanks to the type-hint, the container will instantiate a
152147 // new MessageGenerator and pass it to you!
@@ -167,9 +162,7 @@ each time you ask for it.
167162
168163.. sidebar :: Automatic Service Loading in services.yaml
169164
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:
173166
174167 .. configuration-block ::
175168
@@ -185,10 +178,10 @@ each time you ask for it.
185178
186179 # makes classes in src/ available to be used as services
187180 App\ :
188- resource : ' ../../ src/*'
181+ resource : ' ../src/*'
189182 # you can exclude directories or files
190183 # but if a service is unused, it's removed anyway
191- exclude : ' ../../ src/{Entity,Repository}'
184+ exclude : ' ../src/{Entity,Repository}'
192185
193186 .. code-block :: xml
194187
@@ -204,7 +197,7 @@ each time you ask for it.
204197 <defaults autowire =" true" autoconfigure =" true" public =" false" />
205198
206199 <!-- 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}" />
208201 </services >
209202 </container >
210203
@@ -223,7 +216,7 @@ each time you ask for it.
223216 ;
224217
225218 // $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}');
227220
228221 .. tip ::
229222
@@ -237,15 +230,16 @@ each time you ask for it.
237230 If you'd prefer to manually wire your service, that's totally possible: see
238231 :ref: `services-explicitly-configure-wire-services `.
239232
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::
242236
243237 use App\Service\MessageGenerator;
244238
245239 // accessing services like this only works if you extend Controller
246240 class ProductController extends Controller
247241 {
248- public function newAction ()
242+ public function new ()
249243 {
250244 // only works if your service is public
251245 $messageGenerator = $this->get(MessageGenerator::class);
@@ -256,8 +250,6 @@ be its class name in this case::
256250 }
257251 }
258252
259- However, this only works if you make your service :ref: `public <container-public >`.
260-
261253.. _services-constructor-injection :
262254
263255Injecting Services/Config into a Service
@@ -368,7 +360,7 @@ you can use the service immediately::
368360
369361 use App\Updates\SiteUpdateManager;
370362
371- public function newAction (SiteUpdateManager $siteUpdateManager)
363+ public function new (SiteUpdateManager $siteUpdateManager)
372364 {
373365 // ...
374366
@@ -438,8 +430,8 @@ pass here. No problem! In your configuration, you can explicitly set this argume
438430
439431 # same as before
440432 App\ :
441- resource : ' ../../ src/*'
442- exclude : ' ../../ src/{Entity,Repository}'
433+ resource : ' ../src/*'
434+ exclude : ' ../src/{Entity,Repository}'
443435
444436 # explicitly configure the service
445437 App\Updates\SiteUpdateManager :
@@ -459,7 +451,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
459451 <!-- ... -->
460452
461453 <!-- Same as before -->
462- <prototype namespace =" App\" resource =" ../../ src/*" exclude =" ../ ../src/{Entity,Repository}" />
454+ <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{Entity,Repository}" />
463455
464456 <!-- Explicitly configure the service -->
465457 <service id =" App\Updates\SiteUpdateManager" >
@@ -483,7 +475,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
483475 ->setPublic(false)
484476 ;
485477
486- $this->registerClasses($definition, 'App\\', '../../ src/*', '../ ../src/{Entity,Repository}');
478+ $this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Repository}');
487479
488480 // Explicitly configure the service
489481 $container->getDefinition(SiteUpdateManager::class)
@@ -553,9 +545,9 @@ and reference it with the ``%parameter_name%`` syntax:
553545 // ...
554546 ->setArgument('$adminEmail', '%admin_email%');
555547
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.
559551
560552You can then fetch the parameter in the service::
561553
@@ -573,11 +565,11 @@ You can then fetch the parameter in the service::
573565
574566You can also fetch parameters directly from the container::
575567
576- public function newAction ()
568+ public function new ()
577569 {
578570 // ...
579571
580- // this ONLY works if you extend Controller
572+ // this ONLY works if you extend the base Controller
581573 $adminEmail = $this->container->getParameter('admin_email');
582574
583575 // or a shorter way!
@@ -741,7 +733,7 @@ as a service, and :doc:`tag </service_container/tags>` it with ``twig.extension`
741733 ->addTag('twig.extension');
742734
743735 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 >`,
745737you don't need to do *anything *: the service will be automatically loaded. Then,
746738``autoconfigure `` will add the ``twig.extension `` tag *for * you, because your class
747739implements ``Twig_ExtensionInterface ``. And thanks to ``autowire ``, you can even add
@@ -789,15 +781,15 @@ from the container::
789781
790782 use App\Service\MessageGenerator;
791783
792- public function newAction (MessageGenerator $messageGenerator)
784+ public function new (MessageGenerator $messageGenerator)
793785 {
794786 // type-hinting it as an argument DOES work
795787
796788 // but accessing it directly from the container does NOT Work
797789 $this->container->get(MessageGenerator::class);
798790 }
799791
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 *
801793need to make your service public, just override this setting:
802794
803795.. configuration-block ::
@@ -848,13 +840,13 @@ key. For example, the default Symfony configuration contains this:
848840 # the namespace prefix for classes (must end in \)
849841 App\ :
850842 # create services for all the classes found in this directory...
851- resource : ' ../../ src/*'
843+ resource : ' ../src/*'
852844 # ...except for the classes located in these directories
853- exclude : ' ../../ src/{Entity,Repository}'
845+ exclude : ' ../src/{Entity,Repository}'
854846
855847 # these were imported above, but we want to add some extra config
856848 App\Controller\ :
857- resource : ' ../../ src/Controller'
849+ resource : ' ../src/Controller'
858850 # apply some configuration to these services
859851 public : true
860852 tags : ['controller.service_arguments']
@@ -871,9 +863,9 @@ key. For example, the default Symfony configuration contains this:
871863 <services >
872864 <!-- ... -->
873865
874- <prototype namespace =" App\" resource =" ../../ src/*" exclude =" ../ ../src/{Entity,Repository}" />
866+ <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{Entity,Repository}" />
875867
876- <prototype namespace =" App\Controller\" resource =" ../../ src/Controller" public =" true" >
868+ <prototype namespace =" App\Controller\" resource =" ../src/Controller" public =" true" >
877869 <tag name =" controller.service_arguments" />
878870 </prototype >
879871 </services >
@@ -893,7 +885,7 @@ key. For example, the default Symfony configuration contains this:
893885 ->setPublic(false)
894886 ;
895887
896- $this->registerClasses($definition, 'App\\', '../../ src/*', '../ ../src/{Entity,Repository}');
888+ $this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Repository}');
897889
898890 // Changes default config
899891 $definition
@@ -902,7 +894,7 @@ key. For example, the default Symfony configuration contains this:
902894 ;
903895
904896 // $this is a reference to the current loader
905- $this->registerClasses($definition, 'App\\Controller\\', '../../ src/Controller/*');
897+ $this->registerClasses($definition, 'App\\Controller\\', '../src/Controller/*');
906898
907899 .. tip ::
908900
0 commit comments