First, install the package via composer:
composer require kkiernan/breadcrumbs
If using Laravel, add the service provider and alias to config/app.php
.
'providers' => [
Kiernan\Breadcrumbs\ServiceProvider::class,
],
'aliases' => [
'Breadcrumbs' => \Kiernan\Breadcrumbs\Facade::class,
]
Add breadcrumbs as needed before rendering your view:
Breadcrumbs::add('Posts', action('PostsController@index'));
Breadcrumbs::add('New Post');
Add many breadcrumbs at once if you prefer:
Breadcrumbs::addMany([
['Posts', action('PostsController@index')],
['New Post']
]);
A Bootstrap partial is included to display your breadcrumbs. If using Laravel Blade, you can include the partial in your template:
@include('kkiernan::breadcrumbs');
If you'd like to edit the partial, publish it to resources/views/vendor/kkiernan
:
php artisan vendor:publish --tag=kkiernan
Breadcrumbs can be added dynamically, which is helpful when multiple pages link to a particular page. For example, imagine that both a dashboard and a list of posts link to a post detail view. Consider the following Laravel-centric example in which the first breadcrumb will render as either "Dashboard" or "Posts" depending on the referring page.
// DashboardController@index...
Breadcrumbs::put('posts', 'Dashboard', action('DashboardController@index'));
// PostsController@index...
Breadcrumbs::put('posts', 'Posts', action('DashboardController@index'));
// PostsController@show...
Breadcrumbs::addDynamic('posts');
Breadcrumbs::add($post->title);
If you need to unset a dynamic crumb and prevent it from rendering, simply call the forget method:
Breadcrumbs::forget('posts');