robotsinside/laravel-breadcrumbs
Composer 安装命令:
composer require robotsinside/laravel-breadcrumbs
包简介
Simple breadcrumb implementation for Laravel.
README 文档
README
A simple breadcrumbs implementation for Laravel with flexible class based segment customisation.
Table of contents
- Installation
- Usage
- Configuration
- Package removal
- Todo
- Changelog
- Contributing
- Security
- Credits
- Coffee Time
- License
Installation
Depending on which version of Laravel you're on, you may need to specify which version to install.
| Laravel Version | Package Version |
|---|---|
| 9.0 | ^1.2 |
| 8.0 | ^1.0 |
-
Run
composer require robotsinside/laravel-breadcrumbs. -
Optionally register the service provider in
config/app.php
/* * Package Service Providers... */ \RobotsInside\Breadcrumbs\BreadcrumbsServiceProvider::class,
Auto-discovery is enabled, so this step can be skipped.
- Register the facade for use in your views in
config/app.php
'aliases' => [ ... 'Breadcrumbs' => \RobotsInside\Breadcrumbs\Facades\Breadcrumbs::class, ],
- Publish the config file
php artisan vendor:publish --provider="RobotsInside\Breadcrumbs\BreadcrumbsServiceProvider" --tag="config" - Publish the view files
php artisan vendor:publish --provider="RobotsInside\Breadcrumbs\BreadcrumbsServiceProvider" --tag="views"
Usage
Once installed you can start rendering breadcrumbs immediately.
<div class="container">
{{ Breadcrumbs::render() }}
</div>
For example (using the Bootstrap 4 template) if the URL is http://example.test/about-us/team the output will be:
+-----------+-------------------------------+-----------------------+---------------------------------------------------------------+ | Method | URI | Name | Action | +-----------+-------------------------------+-----------------------+---------------------------------------------------------------+ | GET|HEAD | about-us/team | about.team | Closure |
<div class="container"> <nav aria-label="You are here:" role="navigation"> <ul class="breadcrumb"> <li class="breadcrumb-item"> <a href="http://example.test">Home</a> </li> <li class="breadcrumb-item"> <a href="http://example.test/about-us">About us</a> </li> <li class="breadcrumb-item"> Team </li> </ul> </nav> </div>
The following example includes an injected model.
+-----------+-------------------------------+-----------------------+---------------------------------------------------------------+ | Method | URI | Name | Action | +-----------+-------------------------------+-----------------------+---------------------------------------------------------------+ | GET|HEAD | posts/{post} | posts.show | App\App\Posts\Controllers\PostController@show |
The Post model will be automatically injected into the breadcrumbs class and the label that is used will depend on whether you have a breadcrumb label class defined for that model and which modelAttributes you have listed in your config file.
The modelAttributes key in the config file can be used to define a selection of model attributes for automatic resolution in your breadcrumbs (see Breadcrumb labels). For more complex situations, you can define a BreadcrumbLabel class as shown below. This class contains a model property which is resolved via the route model binding.
<?php namespace App\Breadcrumbs\Labels; use RobotsInside\Breadcrumbs\Label; class PostBreadcrumbLabel extends Label { public function label() { return $this->model->whatever; } }
Don't forget to register your label classes as explained in Breadcrumb labels.
Configuration
The config file should be pretty self explanatory.
Templates
You have the option of choosing a pre-defined template, or creating your own. The provided options are:
- Bootstrap 3 (bootstrap-3)
- Bootstrap 4 (bootstrap-4)
- Bulma (bulma)
- Foundation (foundation)
If you want to provide your own breadcrumbs template, provide the name of your Blade template in the template option:
'template' => [ 'style' => 'custom', 'path' => 'breadcrumbs.custom' // Will look in: 'views.breadcrumbs.custom' ],
Breadcrumb mutators
If you want to define any breadcrumb mutator classes, you need to provide the namespace where the Mutator classes are located. By default this is set to App\\Breadcrumbs\\Mutators\\, but you can change it to suit your project requirements.
Breadcrumb labels
In my own projects I often use name and title model attributes as my breadcrumb labels. Having to define custom breadcrumb labels in these trivial situations is cumbersome. Instead you can define a modelAttributes array in the config file to take care of that.
'modelAttributes' => [ 'name', 'title', 'whatever' ]
If your model matches multiple attributes in this array, the left-most database attribute will be used.
To define some more complex logic for your breadcrumb labels you will need to provide a mapping for your Model => BreadcrumbLabel classes in config/breadcrumbs.php.
'labels' => [ App\Post::class => App\Breadcrumbs\Labels\PostBreadcrumbLabel::class, ],
Removing nodes
Let's say you have an admin area in your app, in that case you probably want to remove the admin segment from your breadcrumbs.
You can achieve this by defining a mutator and calling the remove method in the mutate method. The remove method expects an array, so you can remove multiple nodes if needed.
<?php namespace App\Breadcrumbs\Mutators; use RobotsInside\Breadcrumbs\Mutator; class AdminMutator extends Mutator { public function mutate() { $this->remove(['admin']); } }
{{ Breadcrumbs::mutate('AdminMutator')->render() }}
If the URL is http://example.test/admin/posts/my-post, breadcrumbs will render:
<nav aria-label="You are here:" role="navigation"> <ul class="breadcrumb"> <li class="breadcrumb-item"> <a href="http://example.test"> Home </a> </li> <li class="breadcrumb-item"> <a href="http://example.test/posts"> Posts </a> </li> <li class="breadcrumb-item"> My Post </li> </ul> </nav>
Adding nodes
If for some reason you need to add one or more nodes, you can do that too.
You can achieve this by defining a mutator and calling the add method in the mutate method, for example:
<?php namespace App\Breadcrumbs\Mutators; use RobotsInside\Breadcrumbs\Mutator; class AddPostIndexMutator extends Mutator { public function mutate() { $this->add('All Posts', 'my-post', route('posts.index')); } }
{{ Breadcrumbs::mutate('AddPostIndexMutator')->render() }}
If the URL is http://example.test/my-post, breadcrumbs will render:
<nav aria-label="You are here:" role="navigation"> <ul class="breadcrumb"> <li class="breadcrumb-item"> <a href="http://example.test">Home</a> </li> <li class="breadcrumb-item"> <a href="http://example.test/posts">All Posts</a> </li> <li class="breadcrumb-item"> My Post </li> </ul> </nav>
Package removal
To remove the package:
- Remove the facade in
config/app.php - Delete the
path/to/Breadcrumbsdirectory from your application - Delete all
{{ Breadcrumbs::render }}statements in your views - Delete
config/breadcrumbs.php - Run
composer remove robotsinside/laravel-breadcrumbs
When I developed this package I was faced with a decision on how to let users customise their breadcrumbs. Initally I though I would provide a BreadcrumbsInteface that would need to be implemented on Model classes, but later decided against it since that would mean implementing methods on all of your models that have breadcrumb labels.
Instead, I opted for a config array mapping since that would be easier to remove at a later date if you decide to remove this package from your project at a later date.
Todo
- Write tests
- Finish README
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email robertfrancken@gmail.com instead of using the issue tracker.
Credits
Coffee Time
Will work for ☕☕☕
License
The MIT License (MIT). Please see License File for more information.
robotsinside/laravel-breadcrumbs 适用场景与选型建议
robotsinside/laravel-breadcrumbs 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 985 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 06 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「breadcrumbs」 「robotsinside」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 robotsinside/laravel-breadcrumbs 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 robotsinside/laravel-breadcrumbs 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 robotsinside/laravel-breadcrumbs 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Breadcrumbs integration for Laravel 6
Navigation bundle for Symfony applications
Control pro Nette Framework usnadňující tvorbu menu a drobečkové navigace
Easy breadcrumbs navigation for Pimple powered apps, tested with Silex.
Breadcrumbs
Simple breadcrumbs service for Laravel.
统计信息
- 总下载量: 985
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-06-25