2018-04-17 03:48
浏览 40

Symfony 4 - KNP菜单 - 未调用MenuBuilder

I am upgrading from Symfony 3.3 to Symfony 4.

KNPMenu was running fine under Symfony 3.3, but now I am seeing this exception:

An exception has been thrown during the rendering of a template ("The menu "main" is not defined.").


    public: true
        - { name: app.menu_builder.admin, method: createAdminMenu, alias: admin }
        - { name: app.menu_builder.calendar, method: createCalendarMenu, alias: calendar }
        - { name: app.menu_builder.main, method: createMainMenu, alias: main }
        - { name: app.menu_builder.trailer, method: createTrailerMenu, alias: trailer }
        - { name: app.menu_builder.user, method: createUserMenu, alias: user }


Namespace App\Menu;

use Knp\Menu\FactoryInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\Security\Core\Security;

class MenuBuilder implements ContainerAwareInterface

    use ContainerAwareTrait;

    private $factory;

     * @param FactoryInterface $factory
    public function __construct( FactoryInterface $factory )
        $this->factory = $factory;

I put a die('here'); in the __construct, it is never executed.

Using php bin/console debug:container menu yields:

Information for Service "App\Menu\MenuBuilder"

 ---------------- ------------------------------------------------------------------------- 
  Option           Value                                                                    
 ---------------- ------------------------------------------------------------------------- 
  Service ID       App\Menu\MenuBuilder                                                     
  Class            App\Menu\MenuBuilder                                                     
  Tags             app.menu_builder.admin (method: createAdminMenu, alias: admin)           
                   app.menu_builder.calendar (method: createCalendarMenu, alias: calendar)  
                   app.menu_builder.main (method: createMainMenu, alias: main)              
                   app.menu_builder.trailer (method: createTrailerMenu, alias: trailer)     
                   app.menu_builder.user (method: createUserMenu, alias: user)              
  Public           yes                                                                      
  Synthetic        no                                                                       
  Lazy             no                                                                       
  Shared           yes                                                                      
  Abstract         no                                                                       
  Autowired        yes                                                                      
  Autoconfigured   yes                                                                      
 ---------------- ------------------------------------------------------------------------- 

How can I get MenuBuilder __construct to execute?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanniu3385 2018-04-17 22:59

    Thank you all for your time.

    This gist was helpful:

    This answer was helpful: How to avoid "knp_menu.factory" deprecation?


        class: App\Menu\MenuBuilder
        class: Knp\Menu\MenuItem
        factory: ['@app.menu_builder', 'createMainMenu']
        arguments: { $options: [] }
        - { name:, alias: main }

    I used php bin/console debug:autowiring security to get the security authorization checker

    Then I updated MenuBuilder.php like so:

    Namespace App\Menu;
    use Knp\Menu\FactoryInterface;
    use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
    class MenuBuilder
        private $factory;
        private $security;
         * @param FactoryInterface $factory
        public function __construct( FactoryInterface $factory, AuthorizationCheckerInterface $security )
            $this->factory = $factory;
            $this->security = $security;

    Adding the SecurityAuthorizationChecker allows me to control the menu items offered with this statement:

    if( $this->security->isGranted( 'IS_AUTHENTICATED_FULLY' ) )
    解决 无用
    打赏 举报

相关推荐 更多相似问题