PHP code example of davejamesmiller / laravel-breadcrumbs
1. Go to this page and download the library: Download davejamesmiller/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/ */
davejamesmiller / laravel-breadcrumbs example snippets
// Home
Breadcrumbs::for('home', function ($trail) {
$trail->push('Home', route('home'));
});
// Home > About
Breadcrumbs::for('about', function ($trail) {
$trail->parent('home');
$trail->push('About', route('about'));
});
// Home > Blog
Breadcrumbs::for('blog', function ($trail) {
$trail->parent('home');
$trail->push('Blog', route('blog'));
});
// Home > Blog > [Category]
Breadcrumbs::for('category', function ($trail, $category) {
$trail->parent('blog');
$trail->push($category->title, route('category', $category->id));
});
// Home > Blog > [Category] > [Post]
Breadcrumbs::for('post', function ($trail, $post) {
$trail->parent('category', $post->category);
$trail->push($post->title, route('post', $post->id));
});
'view' => 'breadcrumbs::bootstrap4',
Breadcrumbs::for('home', function ($trail) {
$trail->push('Home', route('home'));
});
Breadcrumbs::for('blog', function ($trail) {
$trail->parent('home');
$trail->push('Blog', route('blog'));
});
Breadcrumbs::for('post', function ($trail, $post) {
$trail->parent('blog');
$trail->push($post->title, route('post', $post));
});
Breadcrumbs::for('category', function ($trail, $category) {
$trail->parent('blog');
foreach ($category->ancestors as $ancestor) {
$trail->push($ancestor->title, route('category', $ancestor->id));
}
$trail->push($category->title, route('category', $category->id));
});
Breadcrumbs::for('category', function ($trail, $category) {
if ($category->parent) {
$trail->parent('category', $category->parent);
} else {
$trail->parent('blog');
}
$trail->push($category->title, route('category', $category->slug));
});
// app/Http/Controllers/PostController.php
use App\Post;
class PostController extends Controller
{
public function show(Post $post) // <-- Implicit model binding happens here
{
return view('post/show', ['post' => $post]);
}
}
// routes/breadcrumbs.php
Breadcrumbs::for('post', function ($trail, $post) { // <-- The same Post model is injected here
$trail->parent('home');
$trail->push($post->title, route('post', $post));
});
use Illuminate\Support\ServiceProvider;
class MyServiceProvider extends ServiceProvider
{
public function register() {}
public function boot()
{
if (class_exists('Breadcrumbs')) {
use DaveJamesMiller\Breadcrumbs\BreadcrumbsManager;
use Illuminate\Support\ServiceProvider;
class MyServiceProvider extends ServiceProvider
{
public function register() {}
public function boot(BreadcrumbsManager $breadcrumbs)
{
$breadcrumbs->register(...);
}
}
use App\Models\Post;
use DaveJamesMiller\Breadcrumbs\BreadcrumbsGenerator;
Breadcrumbs::before(function (BreadcrumbsGenerator $trail) {
// ...
});
Breadcrumbs::for('name', function (BreadcrumbsGenerator $trail, Post $post) {
// ...
});
Breadcrumbs::after(function (BreadcrumbsGenerator $trail) {
// ...
});
Breadcrumbs::macro('resource', function ($name, $title) {
// Home > Blog
Breadcrumbs::for("$name.index", function ($trail) use ($name, $title) {
$trail->parent('home');
$trail->push($title, route("$name.index"));
});
// Home > Blog > New
Breadcrumbs::for("$name.create", function ($trail) use ($name) {
$trail->parent("$name.index");
$trail->push('New', route("$name.create"));
});
// Home > Blog > Post 123
Breadcrumbs::for("$name.show", function ($trail, $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 ($trail, $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');