ramir1/laravel-breadcrumbs-plus
Composer 安装命令:
composer require ramir1/laravel-breadcrumbs-plus
包简介
Define breadcrumbs as classes instead of closures, resolved through the container, with or without registering a name - on top of diglactic/laravel-breadcrumbs.
关键字:
README 文档
README
Adds Breadcrumbs::rule() on top of diglactic/laravel-breadcrumbs —
register a breadcrumb by class name (resolved through the container) instead of a closure, so the class is only
instantiated when that specific breadcrumb is generated, not for every registered page on every request. The method
defaults to __invoke for single-purpose classes and supports constructor dependency injection, matching how
controllers work.
This started as a pull request against diglactic/laravel-breadcrumbs which wasn't merged upstream. Rather than
maintaining a fork of the whole package, this is a small extension package: it requires the vanilla
diglactic/laravel-breadcrumbs and points its manager-class / generator-class config hooks (already built into
that package for this exact purpose) at extended Manager and Generator classes.
Installation
composer require ramir1/laravel-breadcrumbs-plus
Laravel's package auto-discovery registers the service provider automatically. No further setup is required.
Usage
// routes/breadcrumbs.php use Diglactic\Breadcrumbs\Breadcrumbs; use App\Breadcrumbs\PostBreadcrumb; Breadcrumbs::rule('post', PostBreadcrumb::class); Breadcrumbs::rule('post.edit', PostEditBreadcrumb::class, 'edit');
// app/Breadcrumbs/PostBreadcrumb.php namespace App\Breadcrumbs; use Diglactic\Breadcrumbs\Generator; class PostBreadcrumb { public function __construct(private SomeService $service) { } public function __invoke(Generator $trail, Post $post): void { $trail->parent('posts'); $trail->push($post->title, route('posts.show', $post)); } }
Breadcrumbs::rule($name, $class, $method = '__invoke') behaves like Breadcrumbs::for(), except the callback is a
[$class, $method] pair. $class is resolved through the Laravel container the moment the breadcrumb is actually
generated (like a controller), so it can use constructor dependency injection without being instantiated on every
request that merely registers it.
Registering rules from a service provider instead of routes/breadcrumbs.php
routes/breadcrumbs.php is not required. Breadcrumbs::rule() / Breadcrumbs::rules() can be called from anywhere
once the container is available — including a package or module's own ServiceProvider::boot(). This is useful in a
modular app where breadcrumb classes live next to the feature they belong to, rather than in one central file that
has to know about every module:
// Modules/Posts/PostsServiceProvider.php use Diglactic\Breadcrumbs\Breadcrumbs; class PostsServiceProvider extends ServiceProvider { public function boot(): void { Breadcrumbs::rules([ 'posts' => PostsIndexBreadcrumb::class, 'posts.show' => PostBreadcrumb::class, 'posts.edit' => [PostBreadcrumb::class, 'edit'], ]); } }
Breadcrumbs::rules(array $rules) registers many rules in one call. Each entry is either a class name (defaults to
__invoke) or a [$class, $method] pair, keyed by page name — same rules as rule(), just batched.
If config('breadcrumbs.files') is left at its default (routes/breadcrumbs.php) and that file doesn't exist,
diglactic/laravel-breadcrumbs silently skips loading it, so nothing needs to be disabled explicitly.
Rendering by class directly, without registering a name first
Breadcrumbs::renderClass() / Breadcrumbs::generateClass() call a breadcrumb class/method directly, skipping the
rule() registration step entirely. Useful for a one-off page whose breadcrumb isn't reused/shared under a name:
// Instead of: // Breadcrumbs::rule('post.show', PostBreadcrumb::class); // ... // Breadcrumbs::render('post.show', $post); Breadcrumbs::renderClass(PostBreadcrumb::class, '__invoke', $post);
Inside the class, $trail->parentByClass() does the same for an ancestor breadcrumb, instead of $trail->parent('name'):
class PostBreadcrumb { public function __invoke(Generator $trail, Post $post): void { $trail->parentByClass(PostsIndexBreadcrumb::class); // instead of $trail->parent('posts') $trail->push($post->title, route('posts.show', $post)); } }
Both default the method to __invoke, same as rule(). This complements rule() rather than replacing it -
breadcrumbs reused across several pages are still worth registering under a name. It also doesn't affect route-bound
rendering: Breadcrumbs::render() called with no arguments still resolves via the current route's name, which still
needs a matching for()/rule() entry.
How it works
diglactic/laravel-breadcrumbs already resolves its Manager and Generator classes through the container and
exposes config('breadcrumbs.manager-class') / config('breadcrumbs.generator-class') specifically so they can be
subclassed for "more advanced customisations". This package's ServiceProvider sets those two config values to its
own Manager (adds rule()) and Generator (resolves [class, method] callbacks) subclasses — no files from
diglactic/laravel-breadcrumbs are modified or copied.
License
MIT
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-07-12