使用Symfony 3.3.12,我试图将我的菜单构建器用作服务,这在AppBundle / Resources / config / services.yml中配置并在主config.yml中导入为
imports:
- { resource: "@AppBundle/Resources/config/services.yml" }
services.yml
##########################################################################
# Menu Section
##########################################################################
AppBundle\Menu\Builder:
arguments:
$factory: '@knp_menu.factory'
$doctrine: '@doctrine'
$token: '@security.token_storage'
public: true
tags:
- { name: knp_menu.menu_builder, method: sidebarMenu, alias: sidebar }
但是当渲染为
时,我收到此错误{{ knp_menu_render('sidebar', {'template': 'Menu/knp_sidebar_menu.html.twig', 'allow_safe_labels': true, 'currentClass':'active'}) }}
传递给Builder :: __ construct()的参数1必须实现接口 Knp \ Menu \ FactoryInterface,没有给出,调用 /home/demousr/app/vendor/knplabs/knp-menu-bundle/Provider/BuilderAliasProvider.php 在第121行
这是我的建设者
namespace AppBundle\Menu;
use \Knp\Menu\FactoryInterface;
use \AppBundle\Menu\Loader\NodeLoader;
use \Doctrine\Bundle\DoctrineBundle\Registry;
use \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
class Builder {
private $factory;
private $doctrine;
private $token;
public function __construct(FactoryInterface $factory, Registry $doctrine, TokenStorage $token) {
$this->factory = $factory;
$this->doctrine = $doctrine;
$this->token = $token->getToken();
}
public function sidebarMenu(array $options) {
$menu = $this->factory->createItem('root');
$nodeLoader = new NodeLoader($this->factory, $this->token);
$menu->setChildrenAttribute('class', 'nav nav-sidebar');
$em = $this->doctrine->getManager();
$tree = $em->getRepository('AppBundle:Menu\Menu')->findOneByMenu('main');
$roots = $em->getRepository('AppBundle:Menu\MenuItem')->getRootNodesByTree($tree);
foreach($roots as $root){
$item = $nodeLoader->load($root);
if(null !== $item){
$menu->addChild($item);
}
}
return $menu;
}
}
我无法找到错误的地方
答案 0 :(得分:0)
我刚刚在MenuBuilder中更改了类名,并将方法更改为createSidebarMenu,并使用新的类名更改了服务ID,现在它可以正常工作。对我来说只是一个很大的谜。
service.yml
##########################################################################
# Menu Section
##########################################################################
AppBundle\Menu\MenuBuilder:
arguments:
$factory: '@knp_menu.factory'
$doctrine: '@doctrine'
$token: '@security.token_storage'
public: true
tags:
- { name: knp_menu.menu_builder, method: createSidebarMenu, alias: sidebar }
这是 MenuBuilder.php
class MenuBuilder {
private $factory;
private $doctrine;
private $token;
public function __construct(FactoryInterface $factory, Registry $doctrine, TokenStorage $token) {
$this->factory = $factory;
$this->doctrine = $doctrine;
$this->token = $token->getToken();
}
public function createSidebarMenu(array $options) {
# code
}
}