Symfony KnpMenuBundle显示错误[MenuBuilder即服务]

时间:2017-10-11 05:12:28

标签: php symfony symfony-3.3 knpmenubundle

我使用sumfony 3.3.10,我安装了一个新的symfony项目,并使用此命令添加了knpMenuBundle,

Column B

现在我完全按照http://symfony.com/doc/master/bundles/KnpMenuBundle/menu_builder_service.html

中提到的所有内容进行操作

并在default / index.html.twig文件中添加了此行composer require knplabs/knp-menu-bundle "^2.0"

现在当我执行项目时,它显示了我的错误,

{{ knp_menu_render('main') }}

config.yml

[InvalidArgumentException]


Menu builder services must be public but "app.menu_builder" is a private service.

MenuBuilder.php

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: twig

services.yml

    <?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;
     }
 }

我该如何解决它。任何帮助深表感谢。感谢

1 个答案:

答案 0 :(得分:0)

我将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://symfony.com/doc/current/service_container/alias_private.html#marking-services-as-public-private