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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀