huluti/breadcrumbs-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

huluti/breadcrumbs-bundle

Composer 安装命令:

composer require huluti/breadcrumbs-bundle

包简介

Breadcrumbs bundle for Symfony

README 文档

README

This is a fork of mhujer/BreadcrumbsBundle, maintained for newer Symfony versions. The original bundle itself is a fork of sampart/BreadcrumbsBundle.

Packagist Version

Goal

This bundle provides an easy and flexible way to manage breadcrumbs in Symfony applications. It allows developers to dynamically build breadcrumbs for controllers and templates.

Requirements

  • PHP 8.2 or higher
  • Symfony 7.4 or higher

Installation

  1. Install this bundle using Composer:

    composer require huluti/breadcrumbs-bundle

If you're using Symfony Flex, the following steps will be done automatically.

  1. Enable the bundle by adding it to the list of registered bundles in the config/bundles.php file of your project:
// config/bundles.php

return [
    // ...
    Huluti\BreadcrumbsBundle\HulutiBreadcrumbsBundle::class => ['all' => true],
];
  1. Configure the bundle in config/packages/huluti_breadcrumbs.yaml:

    # config/packages/huluti_breadcrumbs.yaml
    huluti_breadcrumbs: ~

That's it for basic configuration. For more options check the Configuration section.

Usage

In your application controller methods:

use Huluti\BreadcrumbsBundle\Model\Breadcrumbs;

public function yourAction(User $user, Breadcrumbs $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()]);
}

or with attributes and PropertyAccess Component

use Huluti\BreadcrumbsBundle\Attribute\Breadcrumb;

#[Breadcrumb(text: 'firstBreadcrumb', route: 'app_main')]
class Controller extends AbstractController{
    
    #[Breadcrumb(text: 'nextBreadcrumb', route: 'app_main')]
    #[Breadcrumb(text: 'nextBreadcrumb', url: '/')]
    #[Breadcrumb(text: 'nextBreadcrumb  {user.name} in {category.name}', route: 'app_main', parameters: ['id' => '{user.id}', 'category' => '{category.name}'])]
    public function yourAction(User $user, Category $category)
    {
    }
}

Then, in your template:

{{ wo_render_breadcrumbs() }}

The last item in the breadcrumbs collection will automatically be rendered as plain text rather than a <a>...</a> tag.

The addItem() method adds an item to the end of the breadcrumbs collection. You can use the prependItem() method to add an item to the beginning of the breadcrumbs collection. This is handy when used in conjunction with hierarchical data (e.g. Doctrine Nested-Set). This example uses categories in a product catalog:

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

    $node = $category;

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

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

If you do not want to generate a URL manually, you can easily add breadcrumb items passing only the route name with any required parameters, using the addRouteItem() and prependRouteItem() methods:

public function yourAction()
{
    $breadcrumbs = $this->get("huluti_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");
}

Configuration

The following default parameters can be overridden in your config/packages/huluti_breadcrumbs.yaml:

# config/packages/huluti_breadcrumbs.yaml
huluti_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:       '@HulutiBreadcrumbs/microdata.html.twig'

These can also be passed as parameters in the view when rendering the breadcrumbs - for example:

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

NOTE: If you need more than one set of breadcrumbs on the same page you can use namespaces. By default, breadcrumbs use the default namespace, but you can add more. To add breadcrumbs to your custom namespace use addNamespaceItem / prependNamespaceItem or addNamespaceRouteItem / prependNamespaceRouteItem methods respectively, for example:

public function yourAction(User $user)
{
    $breadcrumbs = $this->get("huluti_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()]);
}

Then to render the subsection breadcrumbs in your templates, specify this namespace in the options:

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

Advanced Usage

You can add a whole array of objects at once

$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

Example:

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

You can also add a tree path

$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)

NOTE: You can use addNamespaceObjectArray and addNamespaceObjectTree respectively for work with multiple breadcrumbs on the same page.

Overriding the template

There are two methods for doing this.

  1. You can override the template used by copying the Resources/views/microdata.html.twig file out of the bundle and placing it into <your-project>/templates/bundles/HulutiBreadcrumbsBundle, then customising as you see fit. Check the Overriding bundle templates documentation section for more information.

  2. Use the viewTemplate configuration parameter:

    {{ wo_render_breadcrumbs({ viewTemplate: "@HulutiBreadcrumbs/yourBreadcrumbs.html.twig" }) }}

NOTE: If you want to use the JSON-LD format, there's already an existing template at @HulutiBreadcrumbs/json-ld.html.twig. Just set this template as the value for viewTemplate either in your Twig function call (see Step 2 above) or in your bundle configuration.

Contributing

We welcome contributions to this project, including pull requests and issues (and discussions on existing issues).

If you'd like to contribute code but aren't sure what, the issues list is a good place to start. If you're a first-time code contributor, you may find Github's guide to forking projects helpful.

All contributors (whether contributing code, involved in issue discussions, or involved in any other way) must abide by our code of conduct.

huluti/breadcrumbs-bundle 适用场景与选型建议

huluti/breadcrumbs-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 50.92k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2026 年 01 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「breadcrumbs」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 huluti/breadcrumbs-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 huluti/breadcrumbs-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 huluti/breadcrumbs-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 50.92k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 8
  • Watchers: 0
  • Forks: 68
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-21