esign / laravel-breadcrumbs
Manage breadcrumbs within your Laravel application
Installs: 3 479
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: ^8.0
- illuminate/support: ^8.0|^9.0|^10.0|^11.0
- spatie/schema-org: ^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.5
- orchestra/testbench: ^6.0|^7.0|^8.0|^9.0
- phpunit/phpunit: ^9.0|^10.0
README
This package allows you to manage and render breadcrumbs within your Laravel application.
Installation
You can install the package via composer:
composer require esign/laravel-breadcrumbs
The package will automatically register a service provider.
Usage
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')));
Converting to JsonLd
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>
Rendering breadcrumbs
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>
Customizing the breadcrumbs view
In case you want to customize the view components you may publish the views:
php artisan vendor:publish --provider="Esign\Breadcrumbs\BreadcrumbsServiceProvider" --tag="views"
Testing
composer test
License
The MIT License (MIT). Please see License File for more information.