File tree Expand file tree Collapse file tree 1 file changed +19
-3
lines changed Expand file tree Collapse file tree 1 file changed +19
-3
lines changed Original file line number Diff line number Diff line change @@ -349,9 +349,25 @@ Now you can use the service locator by injecting it in any other service:
349
349
->setArguments(array(new Reference('app.command_handler_locator')))
350
350
;
351
351
352
- .. tip ::
352
+ When inside :doc: `/service_container/compiler_passes `, using the
353
+ :method: `Symfony\\ Component\\ DependencyInjection\\ Compiler\\ ServiceLocatorTagPass::register `
354
+ method is the recommended way to create locators for your services. This will save
355
+ you some boilerplate and will automatically share identical locators amongst all the
356
+ services referencing them::
357
+
358
+ use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
359
+ use Symfony\Component\DependencyInjection\ContainerBuilder;
360
+
361
+ public function process(ContainerBuilder $container)
362
+ {
363
+ //...
364
+
365
+ $locateableServices = array(
366
+ //...
367
+ 'logger' => new Reference('logger'),
368
+ );
353
369
354
- If the service locator is not intended to be used by multiple services, it's
355
- better to create and inject it as an anonymous service.
370
+ $myService->addArgument(ServiceLocatorTagPass::register($locateableServices));
371
+ }
356
372
357
373
.. _`Command pattern` : https://en.wikipedia.org/wiki/Command_pattern
You can’t perform that action at this time.
0 commit comments