传递给 Builder::__construct() 的参数 1 必须实现给定的接口 Knp\Menu\FactoryInterface、none
Argument 1 passed to Builder::__construct() must implement interface Knp\Menu\FactoryInterface, none given
使用 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'}) }}
Argument 1 passed to Builder::__construct() must implement interface
Knp\Menu\FactoryInterface, none given, called in
/home/demousr/app/vendor/knplabs/knp-menu-bundle/Provider/BuilderAliasProvider.php
on line 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;
}
}
我找不到我哪里错了
我刚刚更改了 MenuBuilder 中的 class 名称和 createSidebarMenu 的方法,并使用新的 class 名称更改了服务 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
}
}
使用 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'}) }}
Argument 1 passed to Builder::__construct() must implement interface Knp\Menu\FactoryInterface, none given, called in /home/demousr/app/vendor/knplabs/knp-menu-bundle/Provider/BuilderAliasProvider.php on line 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;
}
}
我找不到我哪里错了
我刚刚更改了 MenuBuilder 中的 class 名称和 createSidebarMenu 的方法,并使用新的 class 名称更改了服务 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
}
}