Reputation:
the docs do it like this:
public function process(ContainerBuilder $container)
{
$taggedServices = $container->findTaggedServiceIds(
'acme_mailer.transport'
);
}
But if i attempt to do this in a controller, i get method not found.
public function handlersAction() {
$handlers = $this->container->findTaggedServiceIds(
'quickship.handler'
);
return View::create($handlers);
}
How do i access the ContainerBuilder in a controller?
Upvotes: 1
Views: 1474
Reputation: 12043
findTaggedServiceIds
is a method of ContainerBuilder not of Container
Here is a solution (not the only one) to achieve what you want.
Register your controller as a service, let's say with the id constroller.service and add a method
public function setQuickshipHandlers($handlers){
//Do something with the services IDs
}
Next, you write a compiler pass to get the tagged services and feed your controller/service with them
Should be something like YourBundle/DependencyInjection/Compiler/HandlersPass.php
<?php
namespace YourBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class HandlersPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$controllerServiceDefinition = $container->findDefinition('controller.service');
$handlers = $container->findTaggedServiceIds('quickship.handler');
$controllerServiceDefinition->addMethodCall('setQuickshipHandlers',array_keys($handlers);
}
}
Finally, you add this compiler to your bundle build method. Should be something like YourBundle/YourBundle.php
<?php
namespace YourBundle;
use YourBundle\DependencyInjection\Compiler\HandlersPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class YourBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new HandlersPass());
}
}
Upvotes: 1
Reputation: 2694
You are not allowed to do that after container is compiled.
The way to do that is gather all tagged services in your custom compilerpass and register them in some kind of service.
Then in your controller you will do something like:
$services = $this->get('my_service')->getServices();
Upvotes: 0