zaichaopan/breadcrumb

A package to add dynamic breadcrumb to your laravel app

v0.1-beta 2018-06-21 22:01 UTC

This package is not auto-updated.

Last update: 2024-03-17 02:10:05 UTC


README

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.

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.

// 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.

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.

To implement the interface in the model:

// 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.

To include in your products.show view:

@include('breadcrumb::_nav')

If you want to customize the breadcrumb nav. You can publish the view.

php artisan vendor:publish

Then choose the provider: Zaichaopan\Breadcrumb\BreadcrumbServiceProvider

That is all you need to do to use this package.