PHP code example of diglactic / laravel-breadcrumbs

1. Go to this page and download the library: Download diglactic/laravel-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/ */

    

diglactic / laravel-breadcrumbs example snippets


 // routes/breadcrumbs.php

// Note: Laravel will automatically resolve `Breadcrumbs::` without
// this import. This is nice for IDE syntax and refactoring.
use Diglactic\Breadcrumbs\Breadcrumbs;

// This import is also not home'));
});

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

// Home > Blog > [Category]
Breadcrumbs::for('category', function (BreadcrumbTrail $trail, $category) {
    $trail->parent('blog');
    $trail->push($category->title, route('category', $category));
});

// config/breadcrumbs.php

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

 // routes/breadcrumbs.php

use Diglactic\Breadcrumbs\Breadcrumbs;
use Diglactic\Breadcrumbs\Generator as BreadcrumbTrail;

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

 // routes/breadcrumbs.php

use Diglactic\Breadcrumbs\Breadcrumbs;
use Diglactic\Breadcrumbs\Generator as BreadcrumbTrail;

Breadcrumbs::for('blog', function (BreadcrumbTrail $trail) {
    $trail->parent('home');
    $trail->push('Blog', route('blog'));
});

 // routes/breadcrumbs.php

use App\Models\Post;
use Diglactic\Breadcrumbs\Breadcrumbs;
use Diglactic\Breadcrumbs\Generator as BreadcrumbTrail;

Breadcrumbs::for('post', function (BreadcrumbTrail $trail, Post $post) {
    $trail->parent('blog');
    $trail->push($post->title, route('post', $post));
});

Breadcrumbs::for(
    'post',
    fn (BreadcrumbTrail $trail, Post $post) => $trail
        ->parent('blog')
        ->push($post->title, route('post', $post))
);

 // routes/breadcrumbs.php

use App\Models\Category;
use Diglactic\Breadcrumbs\Breadcrumbs;
use Diglactic\Breadcrumbs\Generator as BreadcrumbTrail;

Breadcrumbs::for('category', function (BreadcrumbTrail $trail, Category $category) {
    $trail->parent('blog');

    foreach ($category->ancestors as $ancestor) {
        $trail->push($ancestor->title, route('category', $ancestor));
    }

    $trail->push($category->title, route('category', $category));
});

 // routes/breadcrumbs.php

use App\Models\Category;
use Diglactic\Breadcrumbs\Breadcrumbs;
use Diglactic\Breadcrumbs\Generator as BreadcrumbTrail;

Breadcrumbs::for('category', function (BreadcrumbTrail $trail, Category $category) {
    if ($category->parent) {
        $trail->parent('category', $category->parent);
    } else {
        $trail->parent('blog');
    }

    $trail->push($category->title, route('category', $category->slug));
});

// config/breadcrumbs.php

'view' => 'partials.breadcrumbs', // --> resources/views/partials/breadcrumbs.blade.php

 // routes/breadcrumbs.php

use App\Models\Post;
use Diglactic\Breadcrumbs\Breadcrumbs;
use Diglactic\Breadcrumbs\Generator as BreadcrumbTrail;

Breadcrumbs::for('post', function (BreadcrumbTrail $trail, Post $post) {
    $trail->parent('home');
    $trail->push($post->title, route('post', $post), ['image' => asset($post->image)]);
});

 // routes/web.php

use Illuminate\Support\Facades\Route;

// Home
Route::name('home')->get('/', 'HomeController@index');

// Home > [Post]
Route::name('post')->get('/post/{id}', 'PostController@show');

 // routes/breadcrumbs.php

use App\Models\Post;
use Diglactic\Breadcrumbs\Breadcrumbs;
use Diglactic\Breadcrumbs\Generator as BreadcrumbTrail;

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

// Home > [Post]
Breadcrumbs::for('post', function (BreadcrumbTrail $trail, Post $post) {
    $trail->parent('home');
    $trail->push($post->title, route('post', $post));
});

Breadcrumbs::for('errors.404', function (BreadcrumbTrail $trail) {
    $trail->parent('home');
    $trail->push('Page Not Found');
});

// config/breadcrumbs.php

'missing-route-bound-breadcrumb-exception' => false,

// config/breadcrumbs.php

'unnamed-route-exception' => false,

 // routes/web.php

use Illuminate\Support\Facades\Route;

Route::name('post')->get('/post/{post}', 'PostController@show');

 // app/Http/Controllers/PostController.php

use App\Http\Controllers\Controller;
use App\Models\Post;
use Illuminate\Contracts\View\View;

class PostController extends Controller
{
    public function show(Post $post): View // <-- Route bound model is injected here
    {
        return view('post/show', ['post' => $post]);
    }
}

 // routes/breadcrumbs.php

use App\Models\Post;
use Diglactic\Breadcrumbs\Breadcrumbs;
use Diglactic\Breadcrumbs\Generator as BreadcrumbTrail;

Breadcrumbs::for('post', function (BreadcrumbTrail $trail, Post $post) { // <-- The same Post model is injected here
    $trail->parent('home');
    $trail->push($post->title, route('post', $post));
});

 // routes/web.php

use App\Http\Controllers\PhotoController;
use Illuminate\Support\Facades\Route;

Route::resource('photo', PhotoController::class);

 // routes/breadcrumbs.php

use App\Models\Photo;
use Diglactic\Breadcrumbs\Breadcrumbs;
use Diglactic\Breadcrumbs\Generator as BreadcrumbTrail;

// Photos
Breadcrumbs::for('photo.index', function (BreadcrumbTrail $trail) {
    $trail->parent('home');
    $trail->push('Photos', route('photo.index'));
});

// Photos > Upload Photo
Breadcrumbs::for('photo.create', function (BreadcrumbTrail $trail) {
    $trail->parent('photo.index');
    $trail->push('Upload Photo', route('photo.create'));
});

// Photos > [Photo Name]
Breadcrumbs::for('photo.show', function (BreadcrumbTrail $trail, Photo $photo) {
    $trail->parent('photo.index');
    $trail->push($photo->title, route('photo.show', $photo));
});

// Photos > [Photo Name] > Edit Photo
Breadcrumbs::for('photo.edit', function (BreadcrumbTrail $trail, Photo $photo) {
    $trail->parent('photo.show', $photo);
    $trail->push('Edit Photo', route('photo.edit', $photo));
});

$trail->push('Sample');

$trail->push('Home', '/', ['icon' => 'home.png']);

 // routes/breadcrumbs.php

use Diglactic\Breadcrumbs\Breadcrumbs;
use Diglactic\Breadcrumbs\Generator as BreadcrumbTrail;

Breadcrumbs::after(function (BreadcrumbTrail $trail) {
    $page = (int) request('page', 1);

    if ($page > 1) {
        $trail->push("Page {$page}");
    }
});

 // routes/breadcrumbs.php

use Diglactic\Breadcrumbs\Breadcrumbs;
use Diglactic\Breadcrumbs\Generator as BreadcrumbTrail;

Breadcrumbs::after(function (BreadcrumbTrail $trail) {
    $page = (int) request('page', 1);

    if ($page > 1) {
        $trail->push("Page {$page}", null, ['current' => false]);
    }
});



use Diglactic\Breadcrumbs\Breadcrumbs;

$current = Breadcrumbs::generate()->where('current', '!==', false)->last();



use Illuminate\Support\Facades\Config;

Config::set('breadcrumbs.view', 'partials.breadcrumbs2');

// config/breadcrumbs.php

'files' => base_path('routes/breadcrumbs.php'),

'files' => [
    base_path('breadcrumbs/admin.php'),
    base_path('breadcrumbs/frontend.php'),
],

'files' => glob(base_path('breadcrumbs/*.php')),

use Illuminate\Support\ServiceProvider;

class MyServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        if (class_exists('Breadcrumbs')) {
            



use Diglactic\Breadcrumbs\Manager;
use Illuminate\Support\ServiceProvider;

class MyServiceProvider extends ServiceProvider
{
    public function boot(Manager $breadcrumbs): void
    {
        $breadcrumbs->for(...);
    }
}



use Diglactic\Breadcrumbs\Breadcrumbs;

Breadcrumbs::macro('pageTitle', function () {
    $title = ($breadcrumb = Breadcrumbs::current()) ? "{$breadcrumb->title} – " : '';

    if (($page = (int) request('page')) > 1) {
        $title .= "Page $page – ";
    }

    return "{$title} - Demo App";
});

// breadcrumbs/config.php

'manager-class' => Diglactic\Breadcrumbs\Manager::class,

'generator-class' => Diglactic\Breadcrumbs\Generator::class,

 // routes/breadcrumbs.php

use App\Models\SomeModel;
use Diglactic\Breadcrumbs\Breadcrumbs;
use Diglactic\Breadcrumbs\Generator as BreadcrumbTrail;

Breadcrumbs::macro('resource', function (string $name, string $title) {
    // Home > Blog
    Breadcrumbs::for("{$name}.index", function (BreadcrumbTrail $trail) use ($name, $title) {
        $trail->parent('home');
        $trail->push($title, route("{$name}.index"));
    });

    // Home > Blog > New
    Breadcrumbs::for("{$name}.create", function (BreadcrumbTrail $trail) use ($name) {
        $trail->parent("{$name}.index");
        $trail->push('New', route("{$name}.create"));
    });

    // Home > Blog > Post 123
    Breadcrumbs::for("{$name}.show", function (BreadcrumbTrail $trail, SomeModel $model) use ($name) {
        $trail->parent("{$name}.index");
        $trail->push($model->title, route("{$name}.show", $model));
    });

    // Home > Blog > Post 123 > Edit
    Breadcrumbs::for("{$name}.edit", function (BreadcrumbTrail $trail, SomeModel $model) use ($name) {
        $trail->parent("{$name}.show", $model);
        $trail->push('Edit', route("{$name}.edit", $model));
    });
});

Breadcrumbs::resource('blog', 'Blog');
Breadcrumbs::resource('photos', 'Photos');
Breadcrumbs::resource('users', 'Users');
bash
php artisan vendor:publish --tag=breadcrumbs-config
bash
php artisan vendor:publish --tag=breadcrumbs-views
bash
php artisan vendor:publish --tag=breadcrumbs-config
blade
<title>
    {{ ($breadcrumb = Breadcrumbs::current()) ? "$breadcrumb->title –" : '' }}
    {{ ($page = (int) request('page')) > 1 ? "Page $page –" : '' }}
    Demo App
</title>
bash
php artisan vendor:publish --tag=breadcrumbs-config