8000 Updated service_container/* articles to Symfony 4 · symfony/symfony-docs@65d6754 · GitHub
[go: up one dir, main page]

Skip to content

Commit 65d6754

Browse files
committed
Updated service_container/* articles to Symfony 4
1 parent 8e9dc96 commit 65d6754

17 files changed

+226
-127
lines changed

service_container.rst

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,10 @@ send emails while another object might help you save things to the database.
1010
Almost *everything* that your app "does" is actually done by one of these objects.
1111
And 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

2318
Fetching 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

263255
Injecting 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

560552
You can then fetch the parameter in the service::
561553

@@ -573,11 +565,11 @@ You can then fetch the parameter in the service::
573565

574566
You 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>`,
745737
you 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
747739
implements ``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*
801793
need 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

service_container/3.3-di-changes.rst

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ Symfony Standard Edition:
4949
# makes classes in src/ available to be used as services
5050
# this creates a service per class whose id is the fully-qualified class name
5151
App\:
52-
resource: '../../src/*'
52+
resource: '../src/*'
5353
# you can exclude directories or files
5454
# but if a service is unused, it's removed anyway
55-
exclude: '../../src/{Entity,Repository}'
55+
exclude: '../src/{Entity,Repository}'
5656
5757
# controllers are imported separately to make sure they're public
5858
# and have a tag that allows actions to type-hint services
5959
App\Controller\:
60-
resource: '../../src/Controller'
60+
resource: '../src/Controller'
6161
tags: ['controller.service_arguments']
6262
6363
# add more services, or override services that need manual wiring
@@ -77,9 +77,9 @@ Symfony Standard Edition:
7777
<services>
7878
<defaults autowire="true" autoconfigure="true" public="false" />
7979
80-
<prototype namespace="App\" resource="../../src/*" exclude="../../src/{Entity,Repository}" />
80+
<prototype namespace="App\" resource="../src/*" exclude="../src/{Entity,Repository}" />
8181
82-
<prototype namespace="App\Controller\" resource="../../src/Controller">
82+
<prototype namespace="App\Controller\" resource="../src/Controller">
8383
<tag name="controller.service_arguments" />
8484
</prototype>
8585
@@ -101,15 +101,15 @@ Symfony Standard Edition:
101101
->setPublic(false)
102102
;
103103
104-
$this->registerClasses($definition, 'App\\', '../../src/*', '../../src/{Entity,Repository}');
104+
$this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Repository}');
105105
106106
// Changes default config
107107
$definition
108108
->addTag('controller.service_arguments')
109109
;
110110
111111
// $this is a reference to the current loader
112-
$this->registerClasses($definition, 'App\\Controller\\', '../../src/Controller/*');
112+
$this->registerClasses($definition, 'App\\Controller\\', '../src/Controller/*');
113113
114114
// add more services, or override services that need manual wiring
115115
@@ -139,10 +139,10 @@ thanks to the following config:
139139
# makes classes in src/ available to be used as services
140140
# this creates a service per class whose id is the fully-qualified class name
141141
App\:
142-
resource: '../../src/*'
142+
resource: '../src/*'
143143
# you can exclude directories or files
144144
# but if a service is unused, it's removed anyway
145-
exclude: '../../src/{Entity,Repository}'
145+
exclude: '../src/{Entity,Repository}'
146146
147147
.. code-block:: xml
148148
@@ -156,7 +156,7 @@ thanks to the following config:
156156
<services>
157157
<!-- ... -->
158158
159-
<prototype namespace="App\" resource="../../src/*" exclude="../../src/{Entity,Repository}" />
159+
<prototype namespace="App\" resource="../src/*" exclude="../src/{Entity,Repository}" />
160160
</services>
161161
</container>
162162
@@ -174,7 +174,7 @@ thanks to the following config:
174174
->setPublic(false)
175175
;
176176
177-
$this->registerClasses($definition, 'App\\', '../../src/*', '../../src/{Entity,Repository}');
177+
$this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Repository}');
178178
179179
This means that every class in ``src/`` is *available* to be used as a
180180
service. And thanks to the ``_defaults`` section at the top of the file, all of
@@ -345,7 +345,7 @@ The third big change is that, in a new Symfony 3.3 project, your controllers are
345345
# controllers are imported separately to make sure they're public
346346
# and have a tag that allows actions to type-hint services
347347
App\Controller\:
348-
resource: '../../src/Controller'
348+
resource: '../src/Controller'
349349
tags: ['controller.service_arguments']
350350
351351
.. code-block:: xml
@@ -360,7 +360,7 @@ The third big change is that, in a new Symfony 3.3 project, your controllers are
360360
<services>
361361
<!-- ... -->
362362
363-
<prototype namespace="App\Controller\" resource="../../src/Controller">
363+
<prototype namespace="App\Controller\" resource="../src/Controller">
364364
<tag name="controller.service_arguments" />
365365
</prototype>
366366
</services>
@@ -372,7 +372,7 @@ The third big change is that, in a new Symfony 3.3 project, your controllers are
372372
373373
// ...
374374
375-
$this->registerClasses($definition, 'App\\Controller\\', '../../src/Controller/*');
375+
$this->registerClasses($definition, 'App\\Controller\\', '../src/Controller/*');
376376
377377
But, you might not even notice this. First, your controllers *can* still extend
378378
the same base ``Controller`` class or a new :ref:`AbstractController <controller-abstract-versus-controller>`.
@@ -720,11 +720,11 @@ You're now ready to automatically register all services in ``src/``
720720
# ...
721721
722722
+ App\:
723-
+ resource: '../../src/*'
724-
+ exclude: '../../src/{Entity,Repository}'
723+
+ resource: '../src/*'
724+
+ exclude: '../src/{Entity,Repository}'
725725
+
726726
+ App\Controller\:
727-
+ resource: '../../src/Controller'
727+
+ resource: '../src/Controller'
728728
+ tags: ['controller.service_arguments']
729729
730730
# ...
@@ -809,11 +809,11 @@ can be autowired. The final configuration looks like this:
809809
public: false
810810
811811
App\:
812-
resource: '../../src/*'
813-
exclude: '../../src/{Entity,Repository}'
812+
resource: '../src/*'
813+
exclude: '../src/{Entity,Repository}'
814814
815815
App\Controller\:
816-
resource: '../../src/Controller'
816+
resource: '../src/Controller'
817817
tags: ['controller.service_arguments']
818818
819819
App\Service\GitHubNotifier:

0 commit comments

Comments
 (0)
0