Package Data | |
---|---|
Maintainer Username: | tabuna |
Maintainer Contact: | bliz48rus@gmail.com (Alexandr Chernyaev) |
Package Create Date: | 2020-04-21 |
Package Last Update: | 2024-03-14 |
Home Page: | https://github.com/tabuna/breadcrumbs |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-23 03:22:26 |
Package Statistics | |
---|---|
Total Downloads: | 2,251,014 |
Monthly Downloads: | 59,056 |
Daily Downloads: | 2,016 |
Total Stars: | 343 |
Total Watchers: | 5 |
Total Forks: | 20 |
Total Open Issues: | 9 |
Breadcrumbs display a list of links indicating the position of the current page in the whole site hierarchy. For example, breadcrumbs like Home / Sample Post / Edit
means the user is viewing an edit page for the "Sample Post". He can click on "Sample Post" to view that page, or he can click on "Home" to return to the homepage.
Home / Sample Post / Edit
This package for the Laravel framework will make it easy to build breadcrumbs in your application.
Run this at the command line:
$ composer require tabuna/breadcrumbs
This will update composer.json
and install the package into the vendor/
directory.
Now you can define breadcrumbs directly in the route files:
<?php
use Tabuna\Breadcrumbs\Trail;
// Home
Route::get('/', fn () => view('home'))
->name('home')
->breadcrumbs(fn (Trail $trail) =>
$trail->push('Home', route('home'))
);
// Home > About
Route::get('/about', fn () => view('home'))
->name('about')
->breadcrumbs(fn (Trail $trail) =>
$trail->parent('home')->push('About', route('about'))
);
You can also get arguments from the request:
<?php
Route::get('/category/{category}', function (Category $category){
//In this example, the category object is your Eloquent model.
//code...
})
->name('category')
->breadcrumbs(fn (Trail $trail, Category $category) =>
$trail->push($category->title, route('category', $category->id))
);
When using resources, a whole group of routes is declared for which you must specify values manually
<?php // routes/web.php
Route::resource('photos', 'PhotoController');
It’s better to specify this in service providers, since route files can be cached
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Tabuna\Breadcrumbs\Breadcrumbs;
use Tabuna\Breadcrumbs\Trail;
class BreadcrumbsServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Breadcrumbs::for('photos.index', fn (Trail $trail) =>
$trail->push('Photos', route('home'))
);
Breadcrumbs::for('photos.create', fn (Trail $trail) =>
$trail
->parent('photos.index', route('photos.index'))
->push('Add new photo', route('home'))
);
}
}
You can do this simply by adding the desired file to the service provider
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class BreadcrumbsServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
require base_path('routes/breadcrumbs.php');
}
}
Then it will be your special file in the route directory:
<?php // routes/breadcrumbs.php
// Photos
Breadcrumbs::for('photo.index', fn (Trail $trail) =>
$trail->parent('home')
->push('Photos', route('photo.index'))
);
In order to display breadcrumbs on the desired page, simply call:
@foreach (Breadcrumbs::current() as $crumbs)
@if ($crumbs->url() && !$loop->last)
<li class="breadcrumb-item">
<a href="{{ $crumbs->url() }}">
{{ $crumbs->title() }}
</a>
</li>
@else
<li class="breadcrumb-item active">
{{ $crumbs->title() }}
</li>
@endif
@endforeach
And results in this output:
Home / About
If you would like to support development by making a donation you can do so here. 😊
For several years, I successfully used the Dave James Miller package to solve my problems, but he stopped developing and supporting it. After a long search for alternatives, I liked the Dwight Watson package, but the isolation of breadcrumbs from the announcement of the routes did not give me rest. That's why I created this package, it uses the code of both previous packages.
The MIT License (MIT). Please see License File for more information.