fragkp/laravel-route-breadcrumb
Composer 安装命令:
composer require fragkp/laravel-route-breadcrumb
包简介
关键字:
README 文档
README
This package tries to give a simple solution for breadcrumbs. Add breadcrumbs direct to your routes and display them in your views.
Installation
You can install the package via composer:
composer require fragkp/laravel-route-breadcrumb
If you want also use the facade to access the main breadcrumb class, add this line to your facades array in config/app.php:
'Breadcrumb' => Fragkp\LaravelRouteBreadcrumb\Facades\Breadcrumb::class,
This package contains some pre-built views for the most active css-frameworks:
If you want to use one of these views, include it in this way:
@include('laravel-breadcrumb::bootstrap3')
To customize the pre-built views, run this command:
php artisan vendor:publish Fragkp\LaravelRouteBreadcrumb\BreadcrumbServiceProvider --tag=views
Note: You could also create your own custom view to display breadcrumb links.
Usage
Defining breadcrumbs
Basic
To add a breadcrumb title to your route, call the breadcrumb method and pass your title.
Route::get('/')->breadcrumb('Your custom title');
Index
On some websites, you wish to have always an index inside your breadcrumbs. Use the breadcrumbIndex method.
This method should only be used once.
Note:
breadcrumbIndexsets also the breadcrumb title for this route.
Route::get('/')->breadcrumbIndex('Start'); Route::get('/foo')->breadcrumb('Your custom title');
Inside groups
The breadcrumb method will also work inside route groups.
Route::get('/')->breadcrumbIndex('Start'); Route::prefix('/foo')->group(function () { Route::get('/bar')->breadcrumb('Your custom title'); });
Group index
Also, it is possible to specify a group index title by calling breadcrumbGroup.
This method should only be used once inside a group.
Note:
breadcrumbGroupsets also the breadcrumb title for this route.
Route::get('/')->breadcrumbIndex('Start'); Route::prefix('/foo')->group(function () { Route::get('/')->breadcrumbGroup('Foo group index'); Route::get('/bar')->breadcrumb('Your custom title'); });
Custom title resolver
If you want to customize your breadcrumb title, you could pass a closure to all breadcrumb methods.
Route::get('/')->breadcrumb(function () { return 'Your custom title'; });
You could also pass a fully qualified class name. This will invoke your class.
Route::get('/')->breadcrumb(YourCustomTitleResolver::class); class YourCustomTitleResolver { public function __invoke() { return 'Your custom title'; } }
You may also pass a callable.
Route::get('/foo/{id}')->breadcrumb([app('my_breadcrumb_resolver'), 'resolve']); // my_breadcrumb_resolver class MyBreadcrumbResolver { public function resolve($id) { $title = $this->repo->findById($id); return $title->getName(); } }
Route parameters
All route parameters will be passed to your resolver. Route model binding is also supported.
Route::get('/{foo}')->breadcrumb(YourCustomTitleResolver::class); class YourCustomTitleResolver { public function __invoke(Foo $foo) { return "Title: {$foo->title}"; } }
Accessing breadcrumb
Links
The links method will return a Collection of BreadcrumbLink.
Note: The array is indexed by the uri.
app(Breadcrumb::class)->links(); // or use here the facade
Example result:
Illuminate\Support\Collection {#266 #items: array:2 [ "/" => Fragkp\LaravelRouteBreadcrumb\BreadcrumbLink {#41 +uri: "/" +title: "Start" } "foo" => Fragkp\LaravelRouteBreadcrumb\BreadcrumbLink {#262 +uri: "foo" +title: "Your custom title" } ] }
Index
The index method will return a single instance of BreadcrumbLink. If you haven't defined any index, null is returned.
app(Breadcrumb::class)->index(); // or use here the facade
Example result:
Fragkp\LaravelRouteBreadcrumb\BreadcrumbLink {#36 +uri: "/" +title: "Start" }
Current
The current method will return a single instance of BreadcrumbLink. If no route is provided (e.g. on errors), null is returned.
app(Breadcrumb::class)->current(); // or use here the facade
Example result:
Fragkp\LaravelRouteBreadcrumb\BreadcrumbLink {#36 +uri: "/" +title: "Your custom title" }
View example
A good way to access the breadcrumb inside your views is to bound it via a View Composer.
For more information about View Composers, have a look at the Laravel docs.
// app/Providers/AppServiceProvider.php class AppServiceProvider extends ServiceProvider { public function boot() { View::composer('your-view', function ($view) { $view->with('breadcrumb', app(Breadcrumb::class)->links()); }); } }
// resources/views/breadcrumb.blade.php <ul> @foreach ($breadcrumb as $link) <li> <a href="{{ url($link->uri) }}">{{ $link->title }}</a> </li> @endforeach </ul>
Testing
./vendor/bin/phpunit
License
MIT License (MIT). Please see License File for more information.
fragkp/laravel-route-breadcrumb 适用场景与选型建议
fragkp/laravel-route-breadcrumb 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.74k 次下载、GitHub Stars 达 70, 最近一次更新时间为 2018 年 05 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「route」 「laravel」 「breadcrumb」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 fragkp/laravel-route-breadcrumb 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 fragkp/laravel-route-breadcrumb 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 fragkp/laravel-route-breadcrumb 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
An extension to Semantic MediaWiki allowing to build breadcrumb links from an attributive property filter
Provide a way to secure accesses to all routes of an symfony application.
Write down your routing mapping at one place
Bundle extension that installs and loads Semantic MediaWiki and associated extensions
A Laravel-like router for the WordPress Rewrite API
A Laravel helper to detect if the current route/path is active.
统计信息
- 总下载量: 11.74k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 70
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-05-13