Skip to content

esign/laravel-breadcrumbs

Repository files navigation

Manage breadcrumbs within your Laravel application

Latest Version on Packagist Total Downloads GitHub Actions

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.