Skip to content

Tell about ServiceLocatorTagPass::register() #9990

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 2, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions service_container/service_subscribers_locators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,25 @@ Now you can use the service locator by injecting it in any other service:
->setArguments(array(new Reference('app.command_handler_locator')))
;

.. tip::
When inside :doc:`/service_container/compiler_passes`, using the
:method:`Symfony\\Component\\DependencyInjection\\Compiler\\ServiceLocatorTagPass::register`
method is the recommended way to create locators for your services. This will save
you some boilerplate and will automatically share identical locators amongst all the
services referencing them::

use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;

public function process(ContainerBuilder $container)
{
//...

$locateableServices = array(
//...
'logger' => new Reference('logger'),
);

If the service locator is not intended to be used by multiple services, it's
better to create and inject it as an anonymous service.
$myService->addArgument(ServiceLocatorTagPass::register($locateableServices));
}

.. _`Command pattern`: https://en.wikipedia.org/wiki/Command_pattern