zetta / menu-bundle
管理菜单
v1.1.6
2014-11-30 01:52 UTC
Requires
- php: >=5.4
- knplabs/knp-menu: ~2.0, >=2.0.0-beta1
- symfony/framework-bundle: ~2
- symfony/security-bundle: ~2
Requires (Dev)
- knplabs/knp-menu-bundle: dev-master
- symfony/yaml: 2.1.*
This package is not auto-updated.
Last update: 2024-09-14 14:30:19 UTC
README
一个菜单统治一切
此捆绑包是KnpMenuBundle的扩展,您可以使用它为系统添加主菜单,这将根据用户角色进行筛选。
特性
- 可以从配置文件
app/config/config.yml
设置菜单 - 菜单链接将自动根据防火墙中定义的规则进行筛选
- 支持JMSSecurityExtraBundle注解以创建筛选器
- 通过@SecureMenu注解与现有菜单集成的功能
要求
- PHP >=5.4
- KnpMenuBundle
- KnpMenu
- JMSSecurityExtraBundle
安装
为了安装此捆绑包,您需要在composer.json
文件中添加依赖项。
//composer.json "require": { "zetta/menu-bundle" : "1.1.*" }
然后您必须在应用程序的内核中注册此捆绑包。
// app/AppKernel.php $bundles = array( .... new Knp\Bundle\MenuBundle\KnpMenuBundle(), new Zetta\MenuBundle\ZettaMenuBundle()
ZettaMenuBundle声明必须在KnpMenuBundle之后
用法
有两种方法可以筛选我们的菜单。
简单方法
如果您使用创建菜单的简单方法
// src/Acme/DemoBundle/Menu/Builder.php namespace Acme\DemoBundle\Menu; use Knp\Menu\FactoryInterface; use Symfony\Component\DependencyInjection\ContainerAware; class Builder extends ContainerAware { public function mainMenu(FactoryInterface $factory, array $options) { $menu = $factory->createItem('root'); $menu->addChild('Home', array('route' => 'homepage')); $menu->addChild('About Me', array( 'route' => 'page_show', 'routeParameters' => array('id' => 42) )); // ... add more children return $menu; } }
只需将注解Zetta\MenuBundle\Annotation\SecureMenu
添加到方法中以筛选项目。
// src/Acme/DemoBundle/Menu/Builder.php namespace Acme\DemoBundle\Menu; use Knp\Menu\FactoryInterface; use Symfony\Component\DependencyInjection\ContainerAware; use Zetta\MenuBundle\Annotation\SecureMenu; class Builder extends ContainerAware { /** * @SecureMenu */ public function mainMenu(FactoryInterface $factory, array $options) { $menu = $factory->createItem('root'); $menu->addChild('Home', array('route' => 'homepage')); $menu->addChild('About Me', array( 'route' => 'page_show', 'routeParameters' => array('id' => 42) )); // ... add more children return $menu; } }
配置方法config.yml
我们在配置文件中定义一个菜单
#app/config/config.yml zetta_menu: menus: admin: childrenAttributes: class: 'nav' children: dashboard: label: 'Dashboard' route: '_welcome' users: label: '<i>Users</i>' uri: '/user/' extras: safe_label: true attributes: id: 'user' linkAttributes: class: 'link' children: new: label: 'New User' uri: '/user/new' archive: label: 'User archive' uri: '/user/archive' catalogs: label: 'Catalogs' route: 'catalogs' children: status: label: 'Status' uri: '/status/list' statistics: label: 'Stats' uri: '/admin/stats' sidebar: #another one ... children: sidebar1: label: "Sidebar 1"
支持KNP菜单选项
- attributes
- linkAttributes
- childrenAttributes
- labelAttributes
- extras
- displayChildren
使用knp twig助手我们可以打印它
{{ knp_menu_render('admin') }}
默认情况下,如果没有在防火墙或注解中存在拒绝规则,将打印完整菜单
- 仪表板
- 用户
- 新用户
- 用户存档
- 目录
- 状态
- 统计数据
为了影响菜单渲染,我们开始定义防火墙中的规则
#app/config/security.yml security: role_hierarchy: ROLE_MANAGER: ROLE_USER ROLE_ADMIN: ROLE_MANAGER ... access_control: - { path: ^/admin/, role: ROLE_ADMIN } - { path: ^/user/new, role: ROLE_MANAGER } - { path: ^/$, role: ROLE_USER }
系统管理员可以查看完整菜单,但是已通过ROLE_USER身份认证的用户只能查看
- 仪表板
- Usuarios
- Usuarios históricos
- Catálogos
- 状态
Annotations
假设我们在routing.yml
中定义了目录路由
#app/config/routing.yml catalogs: pattern: /catalogs/ defaults: {_controller: ExampleBundle:Catalogs:index}
我们在控制器的方法中添加注解
// src/Acme/ExampleBundle/Controller/CatalogsController.php use JMS\SecurityExtraBundle\Annotation\Secure; class CatalogsController{ /** * @Secure(roles="ROLE_MANAGER") */ public function indexAction() { // ... blah } }
具有ROLE_USER角色的相同用户将看到
- 仪表板
- Usuarios
- Usuarios históricos