wemakecustom/menu-part-bundle

此包已被弃用且不再维护。未建议替代包。

使用 KnpMenuBundle 包装的 Symfony Bundle,通过小部分声明菜单

v2.1.2 2016-05-05 21:09 UTC

This package is not auto-updated.

Last update: 2022-07-18 08:52:39 UTC


README

包装 knplabs/knp-menu-bundle,以轻松创建部分自包含菜单提供者。

目标是创建全局菜单(主菜单、顶部、侧边栏等),并让不同的服务在需要时填充它们。例如,一个侧边栏可以提供对当前资源的操作(编辑、删除等)。

不过,此套餐确实需要测试...

警告:此套餐需要 KnpMenuBundle ~2.0

安装

通过 composer 下载并安装套餐

$ php composer.phar require wemakecustom/menu-part-bundle

在 Kernel 中启用 Bundle(及其依赖项)

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Knp\Bundle\MenuBundle\KnpMenuBundle(),
        new WMC\MenuPartBundle\WMCMenuPartBundle(),
    );
}

创建您的第一个菜单

使用 JMSDiExtraBundle,您可以简单地创建一个扩展 MenuPartInterface 的类

<?php
// src/Acme/DemoBundle/Menu/UserMenu.php

namespace Acme\DemoBundle\Menu;

use WMC\MenuPartBundle\Menu\MenuPartInterface;
use JMS\DiExtraBundle\Annotation\Service;
use JMS\DiExtraBundle\Annotation\Tag;
use JMS\DiExtraBundle\Annotation\Inject;
use JMS\DiExtraBundle\Annotation\InjectParams;
use Knp\Menu\MenuItem;

use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

/**
 * @Service(public=false)
 * @Tag("wmc.menu_part", attributes={"menu" = "user"})
 */
class UserMenu implements MenuPartInterface
{
    protected $authorizationChecker;

    /**
     * @InjectParams({
     *     "authorizationChecker" = @Inject("security.authorization_checker")
     * })
     */
    public function __construct(AuthorizationCheckerInterface $authorizationChecker)
    {
        $this->authorizationChecker = $authorizationChecker;
    }

    public function addMenuParts(MenuItem $menu)
    {
        if ($this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
            $menu->addChild('Profile', array('route' => 'fos_user_profile_show'));
        }
    }
}
?>

注意将 menu 属性设置为 user,这是强制性的。您可以创建多个具有相同菜单名称的 MenuPartInterface,它们将自动连接起来。您还可以使用 priority 标签属性来修改顺序。

这是一个服务,因此可以注入所需的内容:request_stacksecurity.authorization_checker 等。

如果您不使用 JMSDiExtraBundle,当然可以使用传统方式。请参阅 HomeMenu.phpmenu.yml

在模板中集成

只需在您的模板中使用菜单名称调用 knp_menu_render

{{ knp_menu_render('user') }}

配置

默认情况下,此套餐不需要任何配置。

创建 MenuPartInterface 时,将自动创建具有默认选项的菜单。但是,您可以在通用套餐配置中指定其他选项。

# app/config/config.yml

wmc_menu_part:
    menus:
        # Applies to all menus, same options as below.
        _all:
            # Useful for Bootstrap menus, etc.
            class: "nav-menu"

        my_menu:
            # Services that will iterates through menu items and possibly hide or modify them.
            visitors:
                - wmc.menu_part.filter.security # ID of a Service implementing MenuVisitorInterface

                ## May also be specified with a priority.
                # the lowest the priority, the earliest the visitor will be run
                # wmc.menu_part.visitor.l10n: { priority: 99 }

            class: "my-awesome-menu"
                ## May also be specified as a list
                # - menu
                # - main-menu

            # Additionnal attributes
            # Use above for classes
            attributes:
                id: "my-menu"

提供的访客

安全过滤器

此过滤器(服务名称:wmc.menu_part.filter.security)将隐藏当前用户无权访问的项目。

此过滤器的当前版本仅依赖于防火墙,不检查 @Security 注解。(待办事项)

本地化(L10n)访客

您首先需要为要翻译的菜单启用访客。

以下示例为所有菜单启用了访客

wmc_menu_part:
  menus:
    _all:
      visitors:
        - wmc.menu_part.visitor.l10n

该访客会在每个菜单项上调用 translator 服务。通过将 translation_parametersextra 设置为 false,可以禁用对所选项目的翻译。

要指定自定义翻译域,可以使用 translation_domainextra

如果使用 transChoice,请指定 translation_numberextra

将使用项目的标签作为翻译键。

示例

// Assuming the L10n visitor is enabled for the current menu.

// Translated with trans, using no parameters and the default translation domain
$menu->addChild('home', ['route' => 'home']);

// Translated with trans, using the %username% parameter
// and the FOSUserBundle translation domain
$menu->addChild('account', [
                   'route' => 'my_account',
                   'extras' => [
                      'translation_parameters' => ['%username%' => $user],
                      'translation_domain'     => 'FOSUserBundle',
                   ]
               ]);

// Translated with transChoice
$menu->addChild('notifications', [
                   'route' => 'my_notifications',
                   'extras' => [
                      'translation_number' => $user->getNotifications()->count(),
                   ]
               ]);

// Not translated
$menu->addChild(':)', [
                   'route' => 'happy',
                   'extras' => [
                      'translation_parameters' => false,
                   ]
               ]);

提供的投票者

投票者用于检测菜单中的当前项。

有关如何使用 RequestVoterPrefixVoter 的信息,请参阅 voters.yml。此文件也可以直接导入到 app/config/config.yml 中。

作者