我使用sumfony 3.3.10,我安装了一个新的symfony项目,并使用以下命令添加了knpMenuBundle,
composer require knplabs/knp-menu-bundle "^2.0"现在,我完全按照这里提到的那样跟踪了一切,service.html
并在default/index.html.twig文件中添加了这一行{{ knp_menu_render('main') }}。
现在,当我执行这个项目时,它会显示出这个错误,
[InvalidArgumentException]
Menu builder services must be public but "app.menu_builder" is a private service.config.yml
knp_menu:
# use "twig: false" to disable the Twig extension and the TwigRenderer
twig:
template: KnpMenuBundle::menu.html.twig
# if true, enables the helper for PHP templates
templating: false
# the renderer to use, list is also available by default
default_renderer: twigMenuBuilder.php
<?php
namespace AppBundle\Menu;
use Knp\Menu\FactoryInterface;
class MenuBuilder
{
private $factory;
/**
* @param FactoryInterface $factory
*
* Add any other dependency you need
*/
public function __construct(FactoryInterface $factory)
{
$this->factory = $factory;
}
public function createMainMenu(array $options)
{
$menu = $this->factory->createItem('root');
$menu->addChild('Home', array('route' => 'homepage'));
// ... add more children
return $menu;
}
}services.yml
app.menu_builder:
class: AppBundle\Menu\MenuBuilder
arguments: ["@knp_menu.factory"]
tags:
- { name: knp_menu.menu_builder, method: createMainMenu, alias: main } # The alias is what is used to retrieve the menu我该怎么解决这个问题。任何帮助都是非常感谢的。谢谢
发布于 2017-10-11 05:56:02
我将public: true添加到services.php中的app.menu_builder服务中,
app.menu_builder:
class: AppBundle\Menu\MenuBuilder
public: true
arguments: ["@knp_menu.factory"]
tags:
- { name: knp_menu.menu_builder, method: createMainMenu, alias: main } # The alias is what is used to retrieve the menu现在一切都很好。
https://stackoverflow.com/questions/46680296
复制相似问题