I am attempting to use a service from another bundle, without creating any dependencies between the bundles. I'm trying to use CompilerPass
but this might not be the best method.
I have created a ParamConverter
in BarBundle which FooBundle should use for it's Controllers.
However I get the error: No converter named foo_bundle.converter_service found for conversion of parameter fooObject
What I have so far:
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);
}
Controller Annotation:
/**
* Show Feed document.
*
* @Route("/{id}/add", name="object_add")
* @ParamConverter("fooObject", converter="foo_bundle.converter_service")
*/
public function addAction(ObjectInterface $fooObject)
If I call $container->get('foo_bundle.converter_service')
in the CompilerPass I can see the object is correctly getting set.
Is it a priority issue? I.e. is the CompilerPass
run after annotations are parsed. Or is this just the wrong approach?
No converter named foo_bundle.converter_service found for conversion of parameter fooObject
. I think I will try a different approach by splitting the paramConverter into method calls within the action. – Avaavadavat