PHP code example of synergitech / laravel-breadcrumbs
1. Go to this page and download the library: Download synergitech/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/ */
synergitech / 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(...);
}
}