Breadcrumb
This package allows you to add dynamic breadcrumb to your laravel app. It is inspired by code course. It can be used in laravel 5.5 or higher., (*1)
Installation
composer require zaichaopan/breadcrumb
Usage
- Implement BreadcrumbLinkInterface in your eloquent model which you want to use to generate breadcrumb.
To better understand how to use this package. Consider the following example: your app can list different products which may belong to different categories., (*2)
// web.php
Route::get('/categories/{category}/products/{product}', 'ProductsController@show');
// ...
class ProductsController extends Controller
{
public function show(Category $category, Product $product)
{
// ...
return view('product.show', compact('product'));
}
}
To generate the breadcrumb link in the products.show view. You need to implement the BreadcrumbLinkInterface in your Category and Product model., (*3)
There is only one method in the BreadcrumbLinkInterface : LinkText. It is used to define the text of the link when generating the breadcrumb. This package needs to know it because you may have model id in the url or you may replace the id with uuid or slug in the url. We don't want to use any one of them for the text for the url when generating the breadcrumb., (*4)
To implement the interface in the model:, (*5)
// Category.php
class Category extends Model implements BreadcrumbLinkInterface
{
public function linkText() : string
{
// you want to display category name in the breadcrumb link
return $this->name;
}
}
// Product.php
class Product extends Model implements BreadcrumbLinkInterface
{
public function linkText(): string
{
// you want to display product name in the breadcrumb link
return $this->name;
}
}
- Include breadcrumb nav link in the view
This package provides a default bootstrap4-style breadcrumb nav partial view., (*6)
To include in your products.show view:, (*7)
@include('breadcrumb::_nav')
If you want to customize the breadcrumb nav. You can publish the view., (*8)
php artisan vendor:publish
Then choose the provider: Zaichaopan\Breadcrumb\BreadcrumbServiceProvider, (*9)
That is all you need to do to use this package., (*10)