This package allows you to manage and render breadcrumbs within your Laravel application.
You can install the package via composer:
composer require esign/laravel-breadcrumbs
The package will automatically register a service provider.
You may start adding breadcrumbs by using the Breadcrumbs
facade.
This is typically done from the controller.
use Esign\Breadcrumbs\Breadcrumb;
use Esign\Breadcrumbs\Facades\Breadcrumbs;
Breadcrumbs::add('Home', 'http://example.com');
Breadcrumbs::add(Breadcrumb::create('Home', 'http://example.com'));
Or add multiple breadcrumbs at once:
use Esign\Breadcrumbs\Breadcrumb;
use Esign\Breadcrumbs\Facades\Breadcrumbs;
Breadcrumbs::add([
'Home' => 'https://www.example.com',
'Blog' => null,
]);
Breadcrumbs::add([
Breadcrumb::create('Home', 'https://www.example.com'),
Breadcrumb::create('Blog'),
]);
You may also prepend breadcrumbs. This may be useful when you always want to add a Home breadcrumb:
use Esign\Breadcrumbs\Breadcrumb;
use Esign\Breadcrumbs\Facades\Breadcrumbs;
Breadcrumbs::prepend('Home', route('home'));
Breadcrumbs::prepend(Breadcrumb::create('Home', route('home')));
To convert your breadcrumbs to JSON-LD you may use the toJsonLd
method.
This method will return an instance of Spatie\SchemaOrg\BreadcrumbList
.
use Esign\Breadcrumbs\Facades\Breadcrumbs;
Breadcrumbs::toJsonLd();
You may cast this instance to a string to render the actual script tag:
(string) Breadcrumbs::toJsonLd(); // <script type="application/ld+json">...</script>
This package ships with a view component to render your breadcrumbs trail:
<x-breadcrumbs />
This will render the following HTML:
use Esign\Breadcrumbs\Facades\Breadcrumbs;
Breadcrumbs::add([
'Home' => 'https://www.example.com',
'Blog' => null,
]);
<ol class="breadcrumbs">
<li class="breadcrumbs__item">
<a href="https://www.example.com" class="breadcrumbs__link">Home</a>
</li>
<li class="breadcrumbs__item">Blog</li>
</ol>
In case you want to customize the view components you may publish the views:
php artisan vendor:publish --provider="Esign\Breadcrumbs\BreadcrumbsServiceProvider" --tag="views"
composer test
The MIT License (MIT). Please see License File for more information.