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));
});
// 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));
});
// 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');
});
// 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));
});
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(...);
}
}
// 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');