I’m using the KnpMenuBundle in my application, and I have followed their instructions to set up my menu as a service.
Whenever I try to render the menu using the twig helper function
{{ knp_menu_render('VhotoHelperBundle:MenuBuilder:mainMenu') }}
It gives me the following error…
An exception has been thrown during the rendering of a template (“Catchable Fatal Error: Argument 1 passed to Vhoto\HelperBundle\Menu\MenuBuilder::__construct() must implement interface Knp\Menu\FactoryInterface, none given
My services.yml file is present in my bundle’s Resources/config directory, and has the following contents:
services:
vhoto.menu_builder:
class: Vhoto\HelperBundle\Menu\MenuBuilder
arguments: ["@knp_menu.factory"]
vhoto.menu.main:
class: Knp\Menu\MenuItem
factory_service: vhoto.menu_builder
factory_method: createMainMenu
arguments: ["@request"]
scope: request
tags:
- { name: knp_menu.menu, alias: main }
I have created an extension class in my bundle’s DependencyInjection directory:
class VhotoHelperExtension extends Extension
{
public function load(array $config, ContainerBuilder $container)
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
}
And have updated my bundle’s build method to register the extension:
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->registerExtension(new VhotoHelperExtension());
}
What I can’t figure out is why my arguments: ["@knp_menu.factory"] config isn’t taking effect, am I missing something in the extension class? I read the chapter on Exposing Semantic Configuration for bundles but it didn’t shed any light.
EDIT: Okay, just realised that I don’t need to manually register my extension in my bundle class. Removed that code from my app now but still getting the same exception.
Okay after re-reading the basic setup, seems everything was right apart from my twig function call.