Add breadcrumbs to your routes
, (*1)
This package tries to give a simple solution for breadcrumbs. Add breadcrumbs direct to your routes and display them in your views., (*2)
Installation
You can install the package via composer:, (*3)
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:, (*4)
'Breadcrumb' => Fragkp\LaravelRouteBreadcrumb\Facades\Breadcrumb::class,
This package contains some pre-built views for the most active css-frameworks:
- Bootstrap 3
- Bootstrap 4
- Bulma
- Foundation 6, (*5)
If you want to use one of these views, include it in this way:, (*6)
@include('laravel-breadcrumb::bootstrap3')
To customize the pre-built views, run this command:, (*7)
php artisan vendor:publish Fragkp\LaravelRouteBreadcrumb\BreadcrumbServiceProvider --tag=views
Note: You could also create your own custom view to display breadcrumb links., (*8)
Usage
Defining breadcrumbs
Basic
To add a breadcrumb title to your route, call the breadcrumb method and pass your title., (*9)
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., (*10)
Note: breadcrumbIndex sets also the breadcrumb title for this route., (*11)
Route::get('/')->breadcrumbIndex('Start');
Route::get('/foo')->breadcrumb('Your custom title');
Inside groups
The breadcrumb method will also work inside route groups., (*12)
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., (*13)
Note: breadcrumbGroup sets also the breadcrumb title for this route., (*14)
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., (*15)
Route::get('/')->breadcrumb(function () {
return 'Your custom title';
});
You could also pass a fully qualified class name. This will invoke your class., (*16)
Route::get('/')->breadcrumb(YourCustomTitleResolver::class);
class YourCustomTitleResolver
{
public function __invoke()
{
return 'Your custom title';
}
}
You may also pass a callable., (*17)
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., (*18)
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., (*19)
Note: The array is indexed by the uri., (*20)
app(Breadcrumb::class)->links(); // or use here the facade
Example result:, (*21)
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., (*22)
app(Breadcrumb::class)->index(); // or use here the facade
Example result:, (*23)
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., (*24)
app(Breadcrumb::class)->current(); // or use here the facade
Example result:, (*25)
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., (*26)
For more information about View Composers, have a look at the Laravel docs., (*27)
// app/Providers/AppServiceProvider.php
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
View::composer('your-view', function ($view) {
$view->with('breadcrumb', app(Breadcrumb::class)->links());
});
}
}
```php
// resources/views/breadcrumb.blade.php, (*28)
## Testing
``` bash
./vendor/bin/phpunit
License
MIT License (MIT). Please see License File for more information., (*29)