firestorm23/navigationbundle

rollerworks/navigation-bundle 的分支

v1.3.0 2015-12-26 13:18 UTC

This package is not auto-updated.

Last update: 2024-09-18 17:39:48 UTC


README

RollerworksNavigationBundle 允许您在应用程序配置中为 KnpMenuBundle 定义菜单结构和面包屑。

要求

您需要至少 PHP 5.3,并且已经安装和配置了 KnpMenuBundle。

安装

注意: RollerworksNavigationBundle 是 KnpMenuBundle 的补充,请确保您已正确安装和配置 KnpMenuBundle。

推荐通过 Composer 安装 RollerworksNavigationBundle。

运行以下命令来添加 rollerworks/navigation-bundle 包:

$ php composer.phar require rollerworks/navigation-bundle

现在,Composer 将自动下载所有必需的文件,并为您安装它们。之后,在 kernel 中启用此包

<?php

// in AppKernel::registerBundles()
$bundles = array(
    // ...
    new Rollerworks\Bundle\NavigationBundle\RollerworksNavigationBundle(),
    // ...
);

使用

表达式

从 Symfony 2.4 开始,您可以通过使用 Symfony ExpressionLanguage 使应用程序配置的某些部分更加动态。由于 RollerworksNavigationBundle 将导航定义注册为依赖注入容器中的服务,因此您还可以为 "parameters" 配置参数使用表达式。

实际上,您可以使用表达式进行导航翻译/路由和服务参数。

@ 开头的参数值将被视为表达式。要标记值为 "文本",请使用双 @,例如 '@@your value',它将转换为 '@your value'

注意: 仅将 第一个 开头的 @@ 转换为单个 @,其他 @ 将保持不变。例如,'my@value' 不会被转换为表达式。

使用专用服务为菜单项/面包屑

如果您的导航是动态的,您还可以使用专用服务。该服务必须返回一个 Knp\Menu\ItemInterface 实例。

定义菜单

菜单定义在 rollerworks_navigation.menus 配置树中,您可以添加您需要的任意数量的菜单。

每个菜单作为 rollerworks_navigation.menu.[menu-name] 在服务容器中注册,并通过 'menu-name' 标记为 KnpMenu 加载器。

rollerworks_navigation:
    menus:
        menu-name:
            template: ~ # optional template, used by the Menu builder
            items:
                item-name: # name of the item, eg. home, products, and such.
                    label:             ~                            # Label of the menu-item, this will be translated with the translator_domain
                    translator_domain: Menus                        # translator domain for the label
                    route:             { name: ~, parameters: { } } # The route.name can not be empty, parameters is optional
                    uri:               ~                            # Alternatively you can use a URI instead of a route
                    items:             []                           # Sub-level items, same as this example (unlimited depth nesting)

                    # If your menu item is to dynamic you may also use a dedicated service.
                    # The service must return a Knp\Menu\ItemInterface instance.
                    service:
                        id:         ~  # service-id, can not be empty
                        method:     ~  # optional method to call on the service
                        parameters: [] # Parameter to pass to the method (same as service container parameters, including Expression support)

                    # Need full control? Speficy an expression get a Knp\Menu\ItemInterface instance
                    # like: service('acme_customer.navigation').getMenu()
                    expression: ~

注意: 您可以为每个菜单项使用静态、服务或表达式。

当使用服务或表达式时,子项必须由返回的 MenuItem 对象提供。

定义面包屑

面包屑定义在 rollerworks_navigation.breadcrumbs 配置树中,您可以定义您需要的任意数量的面包屑。

除了菜单外,更深的面包屑通过名称引用其父级,父级可以进一步引用另一个父级。

提示

将相关的面包屑(s)保存在其自己的包中,并使用 'root-bundle' 进行引用是一个好习惯。

使用 Symfony Config 组件的导入功能从其他包导入配置文件。

在注册之前,最终结构将进行归一化,因此不需要在运行时进行复杂构建或解析。

每个面包屑作为 rollerworks_navigation.breadcrumbs.[breadcrumb-name] 在服务容器中注册,并通过 'breadcrumb-name' 标记为 KnpMenu 加载器。

注意

在整个应用程序中,每个面包屑名称都必须是唯一的。建议使用与服务-id 相同的约定。

例如,'homepage' 可以命名为 'acme_breadcrumbs.homepage'。

rollerworks_navigation:
    breadcrumbs:
        breadcrumb-name: # name of the breadcrumb item. Must be unique though out the application.
            parent:            ~                            # Optional parent breadcrumb to reference (by name)

            # Static configuration
            label:             ~                            # Label of the breadcrumb, this will be translated with the translator_domain
            translator_domain: Breadcrumbs                  # translator domain for the label
            route:             { name: ~, parameters: { } } # The route.name can not be empty, parameters is optional
            uri:               ~                            # Alternatively you can use a URI instead of a route

            # If your breadcrumb is to dynamic you may also use a dedicated service.
            # The service must return a Knp\Menu\ItemInterface instance.
            service:
                id:         ~  # service-id, can not be empty
                method:     ~  # optional method to call on the service
                parameters: [] # Parameter to pass to the method (same as service container parameters, including Expression support)

            # Need full control? Speficy an expression get a Knp\Menu\ItemInterface instance
            # like: service('acme_customer.navigation').getBreadcrumb()
            expression: ~

版本控制

为了提高透明度和对发布周期的洞察,以及努力保持向后兼容性,RollerworksSearch 尽可能遵循语义版本控制指南进行维护。

发布版本将按照以下格式编号

<主版本>.<次版本>.<修订版>

并遵循以下指南

  • 破坏向后兼容性会导致主版本增加(并重置次版本和修订版)
  • 在不破坏向后兼容性的新功能中,次版本会增加(并重置修订版)
  • 错误修复和其它更改会导致修订版增加

有关SemVer的更多信息,请访问 http://semver.org/

许可

本软件包的源代码受MIT许可协议的约束,该协议包含在随源代码一起提供的文件LICENSE中。

贡献

这是一个开源项目。如果您想做出贡献,请阅读贡献指南。如果您正在提交pull request,请遵循提交补丁部分中的指南。