开源软件名称(OpenSource Name):d13r/laravel-breadcrumbs开源软件地址(OpenSource Url):https://github.com/d13r/laravel-breadcrumbs开源编程语言(OpenSource Language):PHP 91.2%开源软件介绍(OpenSource Introduction):Update: 18 October 2020 There is now an official fork of Laravel Breadcrumbs: Blog post: Thanks to Sheng Slogar of Diglactic for volunteering to take this project forward. — Dave As of 18 April 2020, Laravel Breadcrumbs is not being maintained. It will probably keep working for a while - I removed the version constraint from If you want to create your own fork, to fix bugs or add new features, please see the instructions below. The MIT license requires you to keep the copyright notice and license information, but otherwise you can do what you like with the code and documentation. Thanks to the contributors who helped maintain it and add features over the last 7 years - I just don't have the energy for maintaining open source projects (or writing blog posts, or for social media) that I did in 2013, and I've decided it's time to focus on new projects instead. — Dave
|
Laravel Breadcrumbs | Laravel | PHP |
---|---|---|
5.3.2 | 5.6+ | 7.1+ |
5.3.0 – 5.3.1 | 5.6 – 6.x | 7.1+ |
5.2.1 | 5.6 – 5.8 | 7.1+ |
5.1.1 – 5.2.0 | 5.6 – 5.7 | 7.1+ |
5.0.0 – 5.1.0 | 5.6 | 7.1+ |
4.x | 5.5 | 7.0+ |
3.x | 5.0 – 5.4 | 5.4+ |
2.x | 4.0 – 4.2 | 5.3+ |
Note: If you are using an older version, click it in the table above to see the documentation for that version.
Note 2: If you think this documentation can be improved in any way, please edit this file and make a pull request.
Run this at the command line:
composer require davejamesmiller/laravel-breadcrumbs:5.x
This will update composer.json
and install the package into the vendor/
directory.
Create a file called routes/breadcrumbs.php
that looks like this:
<?php
// 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));
});
See the Defining Breadcrumbs section for more details.
By default a Bootstrap-compatible ordered list will be rendered, so if you're using Bootstrap 4 you can skip this step.
First initialise the config file by running this command:
php artisan vendor:publish --tag=breadcrumbs-config
Then open config/breadcrumbs.php
and edit this line:
'view' => 'breadcrumbs::bootstrap4',
The possible values are:
breadcrumbs::bootstrap4
– Bootstrap 4breadcrumbs::bootstrap3
– Bootstrap 3breadcrumbs::bootstrap2
– Bootstrap 2breadcrumbs::bulma
– Bulmabreadcrumbs::foundation6
– Foundation 6breadcrumbs::materialize
– Materializebreadcrumbs::uikit
– UIkitbreadcrumbs::json-ld
– JSON-LD Structured Data (<script> tag, no visible output)partials.breadcrumbs
See the Custom Templates section for more details.
Finally, call Breadcrumbs::render()
in the view for each page, passing it the name of the breadcrumb to use and any additional parameters – for example:
{{ Breadcrumbs::render('home') }}
{{ 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.
Breadcrumbs 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:
The most simple breadcrumb is probably going to be your homepage, which will look something like this:
Breadcrumbs::for('home', function ($trail) {
$trail->push('Home', route('home'));
});
As you can see, you simply call $trail->push($title, $url)
inside the closure.
For generating the URL, you can use any of the standard Laravel URL-generation methods, including:
url('path/to/route')
(URL::to()
)secure_url('path/to/route')
route('routename')
or route('routename', 'param')
or route('routename', ['param1', 'param2'])
(URL::route()
)action('controller@action')
(URL::action()
)'http://www.example.com/'
)This example would be rendered like this:
{{ Breadcrumbs::render('home') }}
And results in this output:
Home
This is another static page, but this has a parent link before it:
Breadcrumbs::for('blog', function ($trail) {
$trail->parent('home');
$trail->push('Blog', route('blog'));
});
It works by calling the closure for the home
breadcrumb defined above.
It would be rendered like this:
{{ Breadcrumbs::render('blog') }}
And results in this output:
Home / Blog
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.
This is a dynamically generated page pulled from the database:
Breadcrumbs::for('post', function ($trail, $post) {
$trail->parent('blog');
$trail->push($post->title, route('post', $post));
});
The $post
object (probably an Eloquent model, but could be anything) would simply be passed in from the view:
{{ Breadcrumbs::render('post', $post) }}
It results in this output:
Tip: You can pass multiple parameters if necessary.
Finally, if you have nested categories or other special requirements, you can call $trail->push()
multiple times:
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));
});
Alternatively you could make a recursive function such as this:
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));
});
Both would be rendered like this:
{{ Breadcrumbs::render('category', $category) }}
And result in this:
Home / Blog / Grandparent Category / Parent Category / Category Title
To customise the HTML, create your own view file (e.g. resources/views/partials/breadcrumbs.blade.php
) like this:
@if (count($breadcrumbs))
<ol class="breadcrumb">
@foreach ($breadcrumbs as $breadcrumb)
@if ($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>
@endif
(See the views/ directory for the built-in templates.)
The view will receive an array called $breadcrumbs
.
Each breadcrumb is an object with the following keys:
title
– The breadcrumb titleurl
– The breadcrumb URL, or null
if none was given$data
(see Custom data)Then update your config file (config/breadcrumbs.php
) with the custom view name, e.g.:
'view' => 'partials.breadcrumbs', #--> resources/views/partials/breadcrumbs.blade.php
Alternatively you can skip the custom view and call Breadcrumbs::generate()
to get the breadcrumbs Collection directly:
@foreach (Breadcrumbs::generate('post', $post) as $breadcrumb)
{{-- ... --}}
@endforeach
Call Breadcrumbs::render()
in the view for each page, passing it the name of the breadcrumb to use and any additional parameters.
In the page (e.g. resources/views/home.blade.php
):
{{ Breadcrumbs::render('home') }}
Or with a parameter:
{{ Breadcrumbs::render('category', $category) }}
In the page (e.g. resources/views/home.blade.php
):
@extends('layout.name')
@section('breadcrumbs')
{{ Breadcrumbs::render('home') }}
@endsection
Or using the shorthand syntax:
@extends('layout.name')
@section('breadcrumbs', Breadcrumbs::render('home'))
And in the layout (e.g. resources/views/layout/name.blade.php
):
@yield('breadcrumbs')
In the page (e.g. resources/views/home.php
):
<?= Breadcrumbs::render('home') ?>
Or use the longhand syntax if you prefer:
<?php echo Breadcrumbs::render('home') ?>
To render breadcrumbs as JSON-LD structured data (usually for SEO reasons), use Breadcrumbs::view()
to render the breadcrumbs::json-ld
template in addition to the normal one. For example:
<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 TrimUrls
middleware.)
To specify an image, add it to the $data
parameter in push()
:
Breadcrumbs::for('post', function ($trail, $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.)
In normal usage you must call Breadcrumbs::render($name, $params...)
to render the breadcrumbs on every page. If you prefer, you can name your breadcrumbs the same as your routes and avoid this duplication...
Make sure each of your routes has a name. For example (routes/web.php
):
// 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.
For each route, create a breadcrumb with the same name and parameters. For example (routes/breadcrumbs.php
):
// Home
Breadcrumbs::for('home', function ($trail) {
$trail->push('Home', route('home'));
});
// Home > [Post]
Breadcrumbs::for('post', function ($trail, $id) {
$post = Post::findOrFail($id);
$trail->parent('home');
$trail->push($post->title, route('post', $post));
});
To add breadcrumbs to a custom 404 Not Found page, use the name errors.404
:
// Error 404
Breadcrumbs::for('errors.404', function ($trail) {
$trail->parent('home');
$trail->push('Page Not Found');
});
Call Breadcrumbs::render()
with no parameters in your layout file (e.g. resources/views/app.blade.php
):
{{ Breadcrumbs::render() }}
This will automatically output breadcrumbs corresponding to the current route. The same applies to Breadcrumbs::generate()
:
$breadcrumbs = Breadcrumbs::generate();
And to Breadcrumbs::view()
:
{{ Breadcrumbs::view('breadcrumbs::json-ld') }}
It will throw an InvalidB
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论