doukuibi9631 2018-04-17 03:48
浏览 43
已采纳

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.").

services.yaml

App\Menu\MenuBuilder:
    public: true
    tags:
        - { 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 }

MenuBuilder

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: https://gist.github.com/lsv/4d8044d21819f28f0dde52a3fb8211a0

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

    services.yaml

    app.menu_builder:
        class: App\Menu\MenuBuilder
    
    app.menu.main:
        class: Knp\Menu\MenuItem
        factory: ['@app.menu_builder', 'createMainMenu']
        arguments: { $options: [] }
        tags:
        - { name: knp_menu.menu, 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' ) )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 yolov9的训练时间
  • ¥15 二叉树遍历没有报错但无法正常运行
  • ¥15 在linux系统下vscode运行robocup3d上场球员报错
  • ¥15 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动
  • ¥15 大一C语言期末考试,求帮助🙏🙏
  • ¥15 ch340驱动未分配COM