PHP code example of daikazu / breadcrumbs

1. Go to this page and download the library: Download daikazu/breadcrumbs library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

daikazu / breadcrumbs example snippets


// app/Providers/AppServiceProvider.php
use Daikazu\Breadcrumbs\Facades\Breadcrumbs;
use Daikazu\Breadcrumbs\Trail;

public function boot(): void
{
    Breadcrumbs::for('home', function (Trail $trail) {
        $trail->push('Home', route('home'));
    });

    Breadcrumbs::for('about', function (Trail $trail) {
        $trail->parent('home')->push('About', route('about'));
    });
}

use Daikazu\Breadcrumbs\Facades\Breadcrumbs;
use Daikazu\Breadcrumbs\Trail;

Breadcrumbs::for('products.index', function (Trail $trail) {
    $trail->push('Home', route('home'))
          ->push('Products', route('products.index'));
});

$trail->push('Products', route('products.index'), ['icon' => 'shopping-bag']);

// Home
Breadcrumbs::for('home', function (Trail $trail) {
    $trail->push('Home', route('home'));
});

// Products index
Breadcrumbs::for('products.index', function (Trail $trail) {
    $trail->parent('home')
          ->push('Products', route('products.index'));
});

// Individual product — chains three levels deep
Breadcrumbs::for('products.show', function (Trail $trail, Product $product) {
    $trail->parent('products.index')
          ->push($product->name, route('products.show', $product));
});

Breadcrumbs::for('products.show', function (Trail $trail, Product $product) {
    $trail->parent('products.index')
          ->push($product->name, route('products.show', $product));
});

Breadcrumbs::for('orders.items.show', function (Trail $trail, Order $order, OrderItem $item) {
    $trail->parent('orders.show', $order)
          ->push("Item #{$item->id}", route('orders.items.show', [$order, $item]));
});

Breadcrumbs::for('blog.post', function (Trail $trail, $post) {
    $trail->parent('blog.index')
          ->push($post->title, route('blog.post', $post->slug));
});

// In a controller or view where $post is already loaded:
Breadcrumbs::current($post);

// function (Trail $trail, $category, $product)
Breadcrumbs::current($category, $product);

use Daikazu\Breadcrumbs\Contracts\HasBreadcrumb;
use Daikazu\Breadcrumbs\Trail;

class Product extends Model implements HasBreadcrumb
{
    public function toBreadcrumb(Trail $trail): void
    {
        $trail->parent('products.index')
              ->push($this->name, route('products.show', $this));
    }
}

'view' => 'breadcrumbs::bootstrap5',

'view' => 'breadcrumbs::tailwind', // or your own view name after customizing

Breadcrumbs::for('about', function (Trail $trail) {
    $trail->home()->push('About', route('about'));
});

$trail->home('Start', route('dashboard'));

use Daikazu\Breadcrumbs\Trail;

$trail = Trail::fromArray([
    ['label' => 'Home', 'url' => 'https://example.com'],
    ['label' => 'Blog', 'url' => 'https://example.com/blog'],
    ['label' => 'My Post', 'url' => 'https://example.com/blog/my-post'],
]);

$trail = Trail::fromJson('[
    {"label": "Home", "url": "https://example.com"},
    {"label": "Blog", "url": "https://example.com/blog"},
    {"label": "My Post", "url": "https://example.com/blog/my-post"}
]');

// Resolves: Home > Clothing > Men > Tops > T-Shirts > Plain Tees
$trail = Breadcrumbs::generate('products.index', $category);

// Truncate to 4 items: Home > … > T-Shirts > Plain Tees
$truncated = $trail->truncate(4);

$trail->truncate(4, '...');

Breadcrumbs::for('categories.show', function (Trail $trail, Category $category) {
    $trail->parent('categories.index')
          ->push($category->name, route('categories.show', $category));
})->cache(ttl: 3600, tags: ['breadcrumbs', 'categories']);

use Daikazu\Breadcrumbs\BreadcrumbCacheObserver;

Category::observe(new BreadcrumbCacheObserver(['breadcrumbs', 'categories']));

// bootstrap/app.php
->withMiddleware(function (Middleware $middleware) {
    $middleware->web(append: [
        \Daikazu\Breadcrumbs\Middleware\SetBreadcrumbs::class,
    ]);
})

Route::middleware(\Daikazu\Breadcrumbs\Middleware\SetBreadcrumbs::class)
    ->group(base_path('routes/web.php'));

'livewire' => env('BREADCRUMBS_LIVEWIRE', true),

// app/Providers/AppServiceProvider.php
use Laravel\Octane\Events\RequestTerminated;

public function boot(): void
{
    $this->app['events']->listen(RequestTerminated::class, function () {
        app('breadcrumbs')->flush();
    });
}
bash
php artisan vendor:publish --tag="breadcrumbs-config"
bash
php artisan vendor:publish --tag="breadcrumbs-views"
bash
php artisan vendor:publish --tag="breadcrumbs-views"
blade
@foreach ($breadcrumbs->truncate(4) as $crumb)
    @if ($crumb->data['truncated'] ?? false)
        <span>{{ $crumb->label }}</span>
    @else
        <a href="{{ $crumb->url }}">{{ $crumb->label }}</a>
    @endif
@endforeach
js
document.addEventListener('livewire:navigate', () => {
    Livewire.dispatch('breadcrumbs:refresh');
});
bash
php artisan vendor:publish --tag="breadcrumbs-config"