unlooped/menu-bundle

简单的菜单构建器

安装次数: 2,362

依赖项: 0

建议者: 0

安全性: 0

星标: 0

关注者: 3

分支: 0

公开问题: 2

类型:symfony-bundle

2.4.0 2023-01-19 11:25 UTC

README

安装

使用 Symfony Flex 的应用程序

打开命令控制台,进入您的项目目录并执行

$ composer require unlooped/menu-bundle

不使用 Symfony Flex 的应用程序

步骤 1:下载组件

打开命令控制台,进入您的项目目录并执行以下命令以下载此组件的最新稳定版本

$ composer require unlooped/menu-bundle

此命令要求您已全局安装 Composer,如 Composer 文档中的安装章节所述。

步骤 2:启用组件

然后,通过将其添加到项目 app/AppKernel.php 文件中注册的组件列表中来启用组件

// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            // ...
            new Unlooped\MenuBundle\UnloopedMenuBundle(),
        ];

        // ...
    }

    // ...
}

使用方法

创建一个扩展 AbstractMenuBuilderService 的服务

目前仅支持 create#NAME#Menu()

<?php

namespace App\Service;

use Doctrine\Common\Annotations\AnnotationException;
use ReflectionException;
use Unlooped\MenuBundle\Exception\NameForMenuAlreadyExistsException;
use Unlooped\MenuBundle\Exception\ShowAndHideAnnotationSetException;
use Unlooped\MenuBundle\Helper\MenuHelper;
use Unlooped\MenuBundle\Service\AbstractMenuBuilderService;

class MenuBuilderService extends AbstractMenuBuilderService 
{

    /**
     * @throws NameForMenuAlreadyExistsException
     * @throws AnnotationException
     * @throws ReflectionException
     * @throws ShowAndHideAnnotationSetException
     */
    public function createMainMenu(): MenuHelper
    {
        return $this->createMenuHelper()
            ->addMenu('Link A', ['route' => 'route_name_a'])
            ->addMenu('Link B', ['route' => 'route_name_b'])
            ->addMenu('Link C', ['route' => 'route_name_c'])
            ;
    }

}

可用选项和默认值(全部可选)

[
    'label'           => null, // null or string
    'attr'            => [], // array
    'route'           => '', // string
    'routeOptions'    => [], // see route options
    'routeAsAbsolute' => true, // bool
    'url'             => '', // string
    'other'           => [], // array
    'visible'         => true, // bool
]

带子菜单的菜单

public function createMainMenu(): MenuHelper
{
    return $this->createMenuHelper()
        ->addSubMenu('Category 1', [options])
            ->addMenu('Link 1 A', ['route' => 'route_name_c1_a'])
            ->addMenu('Link 1 B', ['route' => 'route_name_c1_b'])
            ->addMenu('Link 1 C', ['route' => 'route_name_c1_c'])
        ->end()
        ->addSubMenu('Category 2', [options])
            ->addMenu('Link 2 A', ['route' => 'route_name_c2_a'])
            ->addMenu('Link 2 B', ['route' => 'route_name_c2_b'])
            ->addMenu('Link 2 C', ['route' => 'route_name_c2_c'])
            ->addSubMenu('Category 2 A', ['route' => 'route_name_cat2a'])
                ->addMenu('Link 2A A', ['route' => 'route_name_c2_a'])
                ->addMenu('Link 2A B', ['route' => 'route_name_c2_b'])
                ->addMenu('Link 2A C', ['route' => 'route_name_c2_c'])
            ->end()
        ->end()
        ;
}

渲染菜单

{{ render_menu('main') }}

带选项

{{ render_menu('main', '@UnloopedMenu/bootstrap_4_sidebar_menu.html.twig', {'attr': {'class': 'collapse', 'id': 'docs'}}) }}

渲染面包屑

{{ render_breadcrumbs('main') }}

隐藏或显示

在控制器中的路由方法上使用 HideInMenuShowInMenu 注解

对所有人隐藏链接

/**
 * @Route("/home", name="home")
 *
 * @HideInMenu()
 */
public function dashboard(): Response
{
    return $this->render('dashboard/index.html.twig', [
        'controller_name' => 'DashboardController',
    ]);
}

仅对具有角色 ROLE_GUESTROLE_USER 的用户隐藏菜单

/**
 * @Route("/home", name="home")
 *
 * @HideInMenu(forRoles={"ROLE_GUEST", "ROLE_USER"})
 */
public function dashboard(): Response
{
    return $this->render('dashboard/index.html.twig', [
        'controller_name' => 'DashboardController',
    ]);
}

仅对具有角色 ROLE_ADMINROLE_USER 的用户显示菜单

/**
 * @Route("/home", name="home")
 *
 * @ShowInMenu(forRoles={"ROLE_ADMIN", "ROLE_USER"})
 */
public function dashboard(): Response
{
    return $this->render('dashboard/index.html.twig', [
        'controller_name' => 'DashboardController',
    ]);
}