在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):diglactic/laravel-breadcrumbs开源软件地址(OpenSource Url):https://github.com/diglactic/laravel-breadcrumbs开源编程语言(OpenSource Language):PHP 93.4%开源软件介绍(OpenSource Introduction):IntroductionA simple Laravel-style way to create breadcrumbs. This project is the official fork of the fantastically original Laravel Breadcrumbs by Dave James Miller and wouldn't have been possible without a bunch of awesome day-one contributors. Thanks, all! Table of Contents
Compatibility Chart
For older Laravel versions, you'll need to use the original GitHub project. Getting Started1. Installcomposer require diglactic/laravel-breadcrumbs 2. DefineCreate a file called <?php // 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 required, and you could replace `BreadcrumbTrail $trail`
// with `$trail`. This is nice for IDE type checking and completion.
use Diglactic\Breadcrumbs\Generator as BreadcrumbTrail;
// Home
Breadcrumbs::for('home', function (BreadcrumbTrail $trail) {
$trail->push('Home', route('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));
}); See the Defining Breadcrumbs section for more details. 3. StyleBy default, a Bootstrap 5 breadcrumb list will be rendered. To change this, initialize the config file by running this command: php artisan vendor:publish --tag=breadcrumbs-config Then, open // config/breadcrumbs.php
'view' => 'breadcrumbs::bootstrap5', The possible values are:
See the Custom Templates section for more details. You may also specify a custom view at runtime. 4. OutputCall {{-- resources/views/home.blade.php --}}
{{ Breadcrumbs::render('home') }}
{{-- resources/views/categories/show.blade.php --}}
{{ Breadcrumbs::render('category', $category) }} See the Outputting Breadcrumbs section for other output options, and see Route-Bound Breadcrumbs for a way to link breadcrumb names to route names automatically. Defining BreadcrumbsBreadcrumbs will usually correspond to actions or types of page. For each breadcrumb, you specify a name, the breadcrumb title, and the URL to link it to. Since these are likely to change dynamically, you do this in a closure, and you pass any variables you need into the closure. The following examples should make it clear: Static pagesThe most simple breadcrumb is probably going to be your homepage, which will look something like this: <?php // routes/breadcrumbs.php
use Diglactic\Breadcrumbs\Breadcrumbs;
use Diglactic\Breadcrumbs\Generator as BreadcrumbTrail;
Breadcrumbs::for('home', function (BreadcrumbTrail $trail) {
$trail->push('Home', route('home'));
}); For generating the URL, you can use any of the standard Laravel URL-generation methods, including:
This example would be rendered like this: {{ Breadcrumbs::render('home') }} And result in this output:
Parent linksThis is another static page, but with a parent link before it: <?php // 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'));
}); It works by calling the closure for the It would be rendered like this: {{ Breadcrumbs::render('blog') }} And result in this output:
Note that the default templates do not create a link for the last breadcrumb (the one for the current page), even when a URL is specified. You can override this by creating your own template – see Custom Templates for more details. Dynamic titles and linksThis is a dynamically generated page pulled from the database: <?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('blog');
$trail->push($post->title, route('post', $post));
}); The {{ Breadcrumbs::render('post', $post) }} It results in this output: You can also chain method calls to Breadcrumbs::for(
'post',
fn (BreadcrumbTrail $trail, Post $post) => $trail
->parent('blog')
->push($post->title, route('post', $post))
); Nested categoriesFinally, if you have nested categories or other special requirements, you can call <?php // 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));
}); Alternatively, you could make a recursive function such as this: <?php // 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));
}); Both would be rendered like this: {{ Breadcrumbs::render('category', $category) }} And result in this:
Custom TemplatesCreate a viewTo customize the HTML, create your own view file similar to the following: {{-- resources/views/partials/breadcrumbs.blade.php --}}
@unless ($breadcrumbs->isEmpty())
<ol class="breadcrumb">
@foreach ($breadcrumbs as $breadcrumb)
@if (!is_null($breadcrumb->url) && !$loop->last)
<li class="breadcrumb-item"><a href="{{ $breadcrumb->url }}">{{ $breadcrumb->title }}</a></li>
@else
<li class="breadcrumb-item active">{{ $breadcrumb->title }}</li>
@endif
@endforeach
</ol>
@endunless If you want to work off an existing built-in template, run the following command: php artisan vendor:publish --tag=breadcrumbs-views This will copy all built-in templates into the View dataThe view will receive a Collection
called Each breadcrumb is an object with the following keys:
Update the configThen, update your config file with the custom view name: // config/breadcrumbs.php
'view' => 'partials.breadcrumbs', // --> resources/views/partials/breadcrumbs.blade.php Skipping the viewAlternatively, you can skip the custom view and call @foreach (Breadcrumbs::generate('post', $post) as $breadcrumb)
{{-- ... --}}
@endforeach Outputting BreadcrumbsCall With Blade{{ Breadcrumbs::render('home') }} Or with a parameter: {{ Breadcrumbs::render('category', $category) }} Structured DataTo render breadcrumbs as JSON-LD structured data (
usually for SEO reasons), use <html>
<head>
...
{{ Breadcrumbs::view('breadcrumbs::json-ld', 'category', $category) }}
...
</head>
<body>
...
{{ Breadcrumbs::render('category', $category) }}
...
</body>
</html> (Note: If you use Laravel Page Speed you may need to
disable the To specify an image, add it to the <?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)]);
}); (If you prefer to use Microdata or RDFa you will need to create a custom template.) Route-Bound BreadcrumbsIn normal usage you must call Name your routesMake sure each of your routes has a name. <?php // 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'); For more details, see Named Routes in the Laravel documentation. Name your breadcrumbs to matchFor each route, create a breadcrumb with the same name and parameters. For example: <?php // 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));
}); To add breadcrumbs to a custom 404 Not Found page, use
the name Breadcrumbs::for('errors.404', function (BreadcrumbTrail $trail) {
$trail->parent('home');
$trail->push('Page Not Found');
}); Output breadcrumbs in your layoutCall {{-- resources/views/app.blade.php --}}
{{ Breadcrumbs::render() }} This will automatically output breadcrumbs corresponding to the current route. The same applies
to Route binding exceptionsWe'll throw an php artisan vendor:publish --tag=breadcrumbs-config Then open the newly-created file and set this value: // config/breadcrumbs.php
'missing-route-bound-breadcrumb-exception' => false, Similarly, to prevent it throwing an // config/breadcrumbs.php
'unnamed-route-exception' => false, |