ドキュメントは次のようにします:
public function process(ContainerBuilder $container)
{
$taggedServices = $container->findTaggedServiceIds(
'acme_mailer.transport'
);
}
しかし、コントローラーでこれを実行しようとすると、メソッドが見つかりません。
public function handlersAction() {
$handlers = $this->container->findTaggedServiceIds(
'quickship.handler'
);
return View::create($handlers);
}
コントローラでContainerBuilderにアクセスするにはどうすればよいですか?
findTaggedServiceIds
方法であるContainerBuilderないのコンテナ
これがあなたが望むものを達成するための解決策(唯一のものではありません)です。
コントローラをサービスとして登録します。たとえば、id constroller.serviceを使用して、メソッドを追加します。
public function setQuickshipHandlers($handlers){ //Do something with the services IDs }
次に、コンパイラパスを記述して、タグ付けされたサービスを取得し、コントローラ/サービスにそれらをフィードします
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);
}
}
最後に、このコンパイラをバンドルビルドメソッドに追加します。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());
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加