jbouzekri / config-knp-menu-bundle
使用yaml文件配置项目包中的knp菜单
7.1
2023-03-02 08:56 UTC
Requires
- php: >=7.4
- knplabs/knp-menu-bundle: ~3.0
- symfony/event-dispatcher: ^5.0|^6.0
- symfony/security-core: ^5.0|^6.0
- symfony/yaml: ^5.0|^6.0
Requires (Dev)
- phpunit/phpunit: ^8.0
- roave/security-advisories: dev-latest
- squizlabs/php_codesniffer: ^3.0
README
简介
此包提供了一种在bundle yml配置中配置knp菜单的方式。
有关knp菜单的更多信息,请参阅
此包受到了oro crm中的OroNavigationBundle的启发。
安装
您可以使用composer进行安装。
将仓库添加到项目的composer.json文件中,并运行更新或安装命令。
{ "require": { "jbouzekri/config-knp-menu-bundle": "3.0.0" } }
然后在AppKernel.php中启用KnpMenuBundle
$bundles = array( ... new Knp\Bundle\MenuBundle\KnpMenuBundle(), new Jb\Bundle\ConfigKnpMenuBundle\JbConfigKnpMenuBundle(), );
文档
为了使用此包,您必须在您的bundle中定义一个名为 navigation.yml 的菜单配置文件。
示例
my_mega_menu: tree: first_level_item: label: My first label children: second_level_item: label: My second level
它将为knp菜单工厂配置一个提供者。然后您可以在twig中使用my_mega_menu作为一个经典的knp菜单
{{ knp_menu_render('my_mega_menu') }}
配置
这是可用于项目的配置定义。
my_mega_menu: childrenAttributes: An array of attributes passed to the root ul tag tree: first_level_item: uri: "An uri. Use it if you do not define route parameter" route: "A sf2 route without @" routeParameters: "an array of parameters to pass to the route" label: "My first label" order: An integer to sort the item in his level. attributes: An array of attributes passed to the knp item linkAttributes: An array of attributes passed to the a tag childrenAttributes: An array of attributes passed to the chidlren block labelAttributes: An array of attributes passed to the label tag display: boolean to hide the item displayChildren: boolean to hide the children roles: array of item (string roles) passed to isGranted securityContext method to check if user has rights in menu items extras: An array of extra parameters (for example icon img, additional content etc.) children: # An array of subitems second_level_item: label: My second level
此配置与Knp Menu Item类中可用的方法相匹配
菜单安全性
安全上下文被注入到菜单项提供者中。
对于根菜单项,在您的twig模板中显示或隐藏它。对于子菜单项,如果您没有添加roles键,则它们将显示。否则,它将传递键数组到isGranted方法并检查您是否有对该项的权限。
问题
- 树子配置属性:在navigation.yml文件中,您必须在菜单名称下定义一个tree键。它提供了在配置解析后的另一个级别来保持第一级项键。如果有人知道如何删除它,请告诉我。