shegroup / menu-bundle
此扩展包为Symfony提供了菜单扩展包
7.0.0
2024-02-07 10:42 UTC
Requires
- php: >=8.3
- symfony/framework-bundle: ^7.0
- symfony/twig-bundle: ^7.0
README
MenuBundle 表示在您的Symfony应用程序中实现简单且功能丰富的菜单!
安装
下载扩展包
打开命令行控制台,进入您的项目目录,并执行以下命令以下载此扩展包的最新稳定版本
$ composer require shegroup/menu-bundle
此命令要求您全局安装Composer,如Composer文档中的“安装章节”中所述。
启用扩展包
然后,通过在您的项目的app/AppKernel.php文件中添加以下行来启用扩展包
// app/AppKernel.php // ... class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new SheGroup\MenuBundle\SheGroupMenuBundle(), ); // ... } // ... }
创建您的第一个菜单
示例构建类可能如下所示
<?php declare(strict_types=1); namespace AppBundle\Menu; use SheGroup\MenuBundle\Menu\MenuInterface; final class MainMenu implements MenuInterface { public function getMenu(): array { $contact = ... return [ 'class' => 'sidebar-menu', 'items' => [ [ 'name' => 'Users', 'icon' => 'fa fa-user', 'items' => [ [ 'name' => 'Admins', 'route' => 'admin_core_user_admin_list', 'active_routes' => [ 'admin_core_user_admin_[\w]+', ], ], [ 'name' => 'Clients', 'route' => 'admin_core_user_client_list', 'active_routes' => [ 'admin_core_user_client_[\w]+', static function (Closure $matcher) use ($contact) { if (!$matcher->__invoke('_app.contact[\w\_\.]+')) { return false; } if ($contact instanceof Person) { return PersonType::isClientRelated($contact->getPersonType()); } return $contact instanceof Client; }, ], ], ], ], [ 'name' => 'Groups', 'route' => 'admin_core_group_list', 'icon' => 'fa fa-users', 'active_routes' => [ 'admin_core_group_[\w]+', '_admin.group.[\w\.]', ], ], ], ]; } }
菜单需要实现MenuInterface接口。如果您需要访问请求信息,您的菜单还需要实现RequestAwareInterface接口。
渲染
为了实际渲染菜单,只需在任何模板中的任何地方执行以下操作
{{ renderMenu('AppBundle\\Menu\\MainMenu', 'sidebar') }}
您可以将菜单定义为服务。为了做到这一点,您需要将服务标记为 'she_group.menu' 标签
app.main_menu: class: AppBundle\Menu\MainMenu tags: [ 'she_group.menu' ]
此标签在启用自动配置时自动添加到所有实现MenuInterface接口的类。
为了渲染它
{{ renderMenu('app.main_menu', 'sidebar') }}