slope-it/breadcrumb-bundle
Composer 安装命令:
composer require slope-it/breadcrumb-bundle
包简介
A bundle for generating dynamic breadcrumbs in Symfony applications
README 文档
README
This bundle provides a way to create "dynamic" breadcrumbs in your Symfony applications.
Installation
Composer is the only supported installation method. Run the following to install the latest version from Packagist:
composer require slope-it/breadcrumb-bundle
Or, if you prefer, you can require any version in your composer.json:
{
"require": {
"slope-it/breadcrumb-bundle": "*"
}
}
Usage
1. Load bundle
Once installed, load the bundle in your Kernel class:
// ... class AppKernel extends Kernel { public function registerBundles() { $bundles = [ // ... new SlopeIt\BreadcrumbBundle\SlopeItBreadcrumbBundle(), ]; // ... } // ... }
2. Define breadcrumbs
There are two ways to create a breadcrumb: via code (1) or via attributes (2).
Via code: you can inject the breadcrumb builder in your controller and add breadcrumb items:
<?php use SlopeIt\BreadcrumbBundle\Service\BreadcrumbBuilder; class CoolController extends Controller { public function coolStuffAction(BreadcrumbBuilder $builder) { $builder->addItem('home', 'home_route'); $builder->addItem('$entity.property', 'entity_route'); $builder->addItem('cool_stuff'); // ... } }
Via attributes: just use the #[Breadcrumb] attribute at the class and/or method level. They will be merged (class comes first).
NOTE: The attribute can take either a single item (in the example it's done at the class level) or multiple items (in the example, at the method level).
<?php use SlopeIt\BreadcrumbBundle\Attribute\Breadcrumb; #[Breadcrumb([ 'label' => 'home', 'route' => 'home_route', 'params' => ['p' => 'val'], 'translationDomain' => 'domain', ])] class CoolController extends Controller { #[Breadcrumb([ ['label' => '$entity.property', 'route' => 'entity_route'], ['label' => 'cool_stuff'], ])] public function coolStuffAction() { // ... } }
3. Render breadcrumb
The last step is to use the following Twig function wherever you want the breadcrumb printed in your template:
{{ slope_it_breadcrumb() }}
Regardless of the way you used to create the breadcrumb, the result will be something like:
Home > Value of entity property > Cool stuff
In which the first two items are anchors and the last one is text only.
Furthermore, in case you need to know whether the breadcrumb has items or not, you can use:
{{ slope_it_is_breadcrumb_empty() }}
How the breadcrumb is generated
Under the hood, this is the business logic involved, for each item, in the breadcrumb generation:
labelwill be the printed text. It can be either:- A "static" string (the translator will attempt to translate it by using it as a translation key)
- A special string, prepended with
$. In this case, the breadcrumb label will be extracted from the variable passed to the template. Property paths can be used, e.g.:$variable.property.path
routewill be used to generate the url for the item anchor (if provided). If not provided, the item will not be clickable.paramswill be used to generate the url related to the provided route. It's an associative array where each value can be either:- A "static" string
- A "special" string using the format
$variable.property.path. The treatment is exactly the same as in "label". This is useful to dynamically retrieve url params (e.g. entity ID) starting from view variables.
translationDomainwill be used to translate the key provided in thelabelattribute. Ifnull, the default translation domain will be used. Providefalseif you put a non-translatable string inlabeland you don't want to use it as a translation key.
NOTE: you don't need to pass all the route parameters that are needed by route, as long as these route parameters are already present in the URL for the current request. In other words, if your breadcrumb hierarchical structure somehow "matches" your URL structure.
Example: suppose you have the following routes, with parameters and resulting URLs:
parent_list | | /parents
parent_view | { parent_id: 12345 } | /parents/12345
children_list | { parent_id: 12345 } | /parents/12345/children
child_view | { parent_id: 12345, child_id: 67890 } | /parents/12345/children/67890
child_edit | { parent_id: 12345, child_id: 67890 } | /parents/12345/children/67890/edit
If you are in the action for route children_edit and you want to generate a breadcrumb including all the above steps, you will be able to use the following annotation:
<?php use SlopeIt\BreadcrumbBundle\Attribute\Breadcrumb; #[Breadcrumb([ ['label' => 'parents', 'route' => 'parent_list'], ['label' => '$parent.name', 'route' => 'parent_view'], ['label' => 'children', 'route' => 'children_list'], ['label' => '$child.name', 'route' => 'child_view'], ['label' => 'edit'], ])] public function childrenEditAction($parentID, $childrenID) { // ... }
Note how you don't have to provide the route parameters (since the current request already has them all). It would work the same if you build it via code instead.
Override breadcrumb template
The bundle default template for rendering breadcrumb can be overridden by adding the following lines to the config.yml of your application:
slope_it_breadcrumb: template: YourBundle::breadcrumb.html.twig
If you intend to do this, it's recommended to copy Resources/views/breadcrumb.html.twig to your bundle and customize it.
However, in your template you'll just have to iterate over the items variable to render your custom breadcrumb.
How to contribute
- Did you find and fix any bugs in the existing code?
- Do you want to contribute a new feature?
- Do you think documentation can be improved?
Under any of these circumstances, please fork this repo and create a pull request. We are more than happy to accept contributions!
slope-it/breadcrumb-bundle 适用场景与选型建议
slope-it/breadcrumb-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 72.04k 次下载、GitHub Stars 达 18, 最近一次更新时间为 2021 年 05 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「bundle」 「breadcrumbs」 「breadcrumb」 「slope」 「slope.it」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 slope-it/breadcrumb-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 slope-it/breadcrumb-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 slope-it/breadcrumb-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Breadcrumbs integration for Laravel 6
2lenet/EasyAdminPlusBundle
The bundle for easy using json-rpc api on your project
Navigation bundle for Symfony applications
Provide a way to secure accesses to all routes of an symfony application.
DMS Meetup API Bundle, enables Meetup API clients in services
统计信息
- 总下载量: 72.04k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 18
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-05-19