whiteoctober/breadcrumbs-bundle

此包已被废弃,不再维护。作者建议使用mhujer/breadcrumbs-bundle包。

适用于Symfony2的小型面包屑包

安装量2,407,840

依赖者: 16

建议者: 1

安全性: 0

星星: 189

关注者: 26

分支: 68

开放问题: 9

类型:symfony-bundle

1.4.5 2019-10-18 12:03 UTC

This package is auto-updated.

Last update: 2020-11-15 19:24:50 UTC


README

警告:此项目已不再维护。如果您正在使用Symfony >= 4.3,您可能需要使用此分支

安装

  1. 如果您还没有配置模板,请为您的应用程序配置模板。例如

    # app/config/config.yml (Symfony <=3)
    framework:
        templating:
            engines: ['twig']
    
    # config/packages/framework.yaml (Symfony 4)
    templating:
        engines: ['twig']
  2. 使用Composer安装此包

    composer require whiteoctober/breadcrumbs-bundle
  3. 将此包添加到您的应用程序内核

    // app/AppKernel.php
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new WhiteOctober\BreadcrumbsBundle\WhiteOctoberBreadcrumbsBundle(),
            // ...
        );
    }

    如果您使用的是Symfony 4,此步骤将由Symfony Flex为您完成。

  4. 在您的配置中配置此包

    # app/config/config.yml
    white_october_breadcrumbs: ~

基本配置到此为止。有关更多选项,请参阅配置部分。

使用

在您的应用程序控制器方法中

public function yourAction(User $user)
{
    $breadcrumbs = $this->get("white_october_breadcrumbs");
    
    // Simple example
    $breadcrumbs->addItem("Home", $this->get("router")->generate("index"));

    // Example without URL
    $breadcrumbs->addItem("Some text without link");

    // Example with parameter injected into translation "user.profile"
    $breadcrumbs->addItem($txt, $url, ["%user%" => $user->getName()]);
}

对于Symfony 4,请通过依赖注入而不是使用get来获取服务

use WhiteOctober\BreadcrumbsBundle\Model\Breadcrumbs;

class YourController extends AbstractController
{
    public function yourAction(Breadcrumbs $breadcrumbs)
    {
      // ...
    }
}

然后,在您的模板中

{{ wo_render_breadcrumbs() }}

面包屑集合中的最后一项将自动以纯文本形式渲染,而不是<a>...</a>标签。

addItem()方法将一个项目添加到面包屑集合的末尾。您可以使用prependItem()方法将项目添加到面包屑集合的开头。当与分层数据(例如,Doctrine Nested-Set)一起使用时,这很有用。此示例使用产品目录中的类别

public function yourAction(Category $category)
{
    $breadcrumbs = $this->get("white_october_breadcrumbs");

    $node = $category;

    while ($node) {
        $breadcrumbs->prependItem($node->getName(), "<category URL>");

        $node = $node->getParent();
    }
}

如果您不想手动生成URL,您可以通过使用addRouteItem()prependRouteItem()方法仅传递路由名称和任何必需参数,轻松添加面包屑项

public function yourAction()
{
    $breadcrumbs = $this->get("white_october_breadcrumbs");
    
    // Pass "_demo" route name without any parameters
    $breadcrumbs->addRouteItem("Demo", "_demo");

    // Pass "_demo_hello" route name with route parameters
    $breadcrumbs->addRouteItem("Hello Breadcrumbs", "_demo_hello", [
        'name' => 'Breadcrumbs',
    ]);

    // Add "homepage" route link at the start of the breadcrumbs
    $breadcrumbs->prependRouteItem("Home", "homepage");
}

配置

以下默认参数可以在您的config.yml或类似文件中覆盖

# app/config/config.yml
white_october_breadcrumbs:
    separator:          '/'
    separatorClass:     'separator'
    listId:             'wo-breadcrumbs'
    listClass:          'breadcrumb'
    itemClass:          ''
    linkRel:            ''
    locale:             ~ # defaults to null, so the default locale is used
    translation_domain: ~ # defaults to null, so the default domain is used
    viewTemplate:       'WhiteOctoberBreadcrumbsBundle::microdata.html.twig'

这些也可以在渲染面包屑时作为参数传递到视图中 - 例如

{{ wo_render_breadcrumbs({separator: '>', listId: 'breadcrumbs'}) }}

注意:如果您在同一个页面上需要多个面包屑集,您可以使用命名空间。默认情况下,面包屑使用default命名空间,但您可以添加更多。要将面包屑添加到您的自定义命名空间,请使用addNamespaceItem / prependNamespaceItemaddNamespaceRouteItem / prependNamespaceRouteItem方法,例如

public function yourAction(User $user)
{
    $breadcrumbs = $this->get("white_october_breadcrumbs");

    // Simple example
    $breadcrumbs->prependNamespaceItem("subsection", "Home", $this->get("router")->generate("index"));

    // Example without URL
    $breadcrumbs->addNamespaceItem("subsection", "Some text without link");

    // Example with parameter injected into translation "user.profile"
    $breadcrumbs->addNamespaceItem("subsection", $txt, $url, ["%user%" => $user->getName()]);
    
    // Example with route name with required parameters
    $breadcrumbs->addNamespaceRouteItem("subsection", $user->getName(), "user_show", ["id" => $user->getId()]);
}

然后在您的模板中渲染subsection面包屑时,指定此命名空间作为选项

{{ wo_render_breadcrumbs({namespace: "subsection"}) }}

高级使用

您一次可以添加整个对象数组

$breadcrumbs->addObjectArray(array $objects, $text, $url, $translationParameters);
objects:            array of objects
text:               name of object property or closure
url:                name of URL property or closure

示例

$that = $this;
$breadcrumbs->addObjectArray($selectedPath, "name", function($object) use ($that) {
    return $that->generateUrl('_object_index', ['slug' => $object->getSlug()]);
});

您还可以添加一个路径树

$breadcrumbs->addObjectTree($object, $text, $url = "", $parent = 'parent', array $translationParameters = [], $firstPosition = -1)
object:             object to start with
text:               name of object property or closure
url:                name of URL property or closure
parent:             name of parent property or closure
firstPosition:      position to start inserting items (-1 = determine automatically)

注意:您可以使用addNamespaceObjectArrayaddNamespaceObjectTree分别处理同一页上的多个面包屑。

覆盖模板

有两种方法可以做到这一点。

  1. 您可以通过复制 Resources/views/microdata.html.twig 文件到 app/Resources/WhiteOctoberBreadcrumbsBundle/views 目录中,然后根据需要自定义它来覆盖使用的模板。有关更多信息,请参阅覆盖包模板文档部分。

  2. 使用 viewTemplate 配置参数

    {{ wo_render_breadcrumbs({ viewTemplate: "YourOwnBundle::yourBreadcrumbs.html.twig" }) }}

注意:如果您想使用 JSON-LD 格式,已存在一个模板在 WhiteOctoberBreadcrumbsBundle::json-ld.html.twig。只需将此模板设置为 viewTemplate 的值即可,无论是在您的 Twig 函数调用中(见上步骤 2)还是在您的包 配置 中。

(该项目最初位于 https://github.com/whiteoctober/BreadcrumbsBundle