我正在尝试使用另一个 bundle 中的服务,而不创建 bundle 之间的任何依赖关系。我正在尝试使用 CompilerPass,但这可能不是最佳方法。
我在 BarBundle 中创建了 ParamConverter,FooBundle 应该为其控制器使用。
但是我收到以下错误消息:
app/config/config.yml
我在 BarBundle 中创建了 ParamConverter,FooBundle 应该为其控制器使用。
但是我收到以下错误消息:
No converter named foo_bundle.converter_service found for conversion of parameter fooObject
到目前为止我已经有:app/config/config.yml
foo_bundle:
converter_service: bar_bundle.converter.object
src/BarBundle/config/service.yml
services:
bar_bundle.converter.object:
class: BarBundle\ParamConverter\ObjectParamConverter
tags:
- { name: request.param_converter, priority: 0, converter: bar_bundle.converter.object }
src/FooBundle/DependencyInjection/FooBundleExtension.php
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
$container->setParameter('foo_bundle.converter_service.id', $config['converter_service']);
}
src/FooBundle/FooBundle.php
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new CompilerPass());
}
src/FooBundle/DependencyInjection/CompilerPass.php
public function process(ContainerBuilder $container)
{
$convertserService = $container->getDefinition($container->getParameter('foo_bundle.converter_service.id'));
$container->setDefinition('foo_bundle.converter_service', $convertserService);
}
控制器注解:
/**
* Show Feed document.
*
* @Route("/{id}/add", name="object_add")
* @ParamConverter("fooObject", converter="foo_bundle.converter_service")
*/
public function addAction(ObjectInterface $fooObject)
如果我在CompilerPass中调用$container->get('foo_bundle.converter_service')
,我可以看到对象正确地被设置。
这是一个优先级问题吗?也就是说,CompilerPass
是在解析注释之后运行的。还是这种方法不对呢?
No converter named foo_bundle.converter_service found for conversion of parameter fooObject
。我想我会尝试另一种方法,将 paramConverter 拆分为操作内的方法调用。 - lookbadgers