Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tabuna/breadcrumbs
Laravel Breadcrumbs - An easy way to add breadcrumbs to your @Laravel app.
https://github.com/tabuna/breadcrumbs
breadcrumb breadcrumbs hacktoberfest laravel laravel-breadcrumbs
Last synced: 25 days ago
JSON representation
Laravel Breadcrumbs - An easy way to add breadcrumbs to your @Laravel app.
- Host: GitHub
- URL: https://github.com/tabuna/breadcrumbs
- Owner: tabuna
- License: mit
- Created: 2020-04-21T17:54:21.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-03-14T02:03:06.000Z (8 months ago)
- Last Synced: 2024-09-30T13:42:13.297Z (about 1 month ago)
- Topics: breadcrumb, breadcrumbs, hacktoberfest, laravel, laravel-breadcrumbs
- Language: PHP
- Homepage: https://github.com/tabuna/breadcrumbs
- Size: 68.4 KB
- Stars: 340
- Watchers: 5
- Forks: 20
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Introduction
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 click on "Home" to return to the homepage.
> [Home](#) / [Sample Post](#) / Edit
This package for the [Laravel framework](https://laravel.com/) will make it easy to build breadcrumbs in your application.
## Installation
Run this at the command line:
```php
$ composer require tabuna/breadcrumbs
```This will update `composer.json` and install the package into the `vendor/` directory.
## Define your breadcrumbs
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))
);
```## Route detection
The package tries to reduce the number of lines needed. For this, you can skip passing the results of the `route()` methods.
The following two declarations will be equivalent:```php
Route::get('/', fn () => view('home'))
->name('home')
->breadcrumbs(fn (Trail $trail) =>
$trail->push('Home', route('home'))
);Route::get('/', fn () => view('home'))
->name('home')
->breadcrumbs(fn (Trail $trail) =>
$trail->push('Home', 'home')
);
```## Like to use a separate route file?
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'))
);
```## Route resource
When using resources, a whole group of routes is declared for which you must specify values manually
```php
// routes/web.phpRoute::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'))
);
}
}
```## Output the breadcrumbs use Blade Component
You can use the output component:
```blade
```
To define classes of list items, you can specify:
```blade
```
You can also pass parameters:
```blade
```
And call named routes explicitly:
```blade
```
## Output the breadcrumbs use Blade view
In order to display breadcrumbs on the desired page, simply call:
```blade
@if(Breadcrumbs::has())
@foreach (Breadcrumbs::current() as $crumbs)
@if ($crumbs->url() && !$loop->last)
{{ $crumbs->title() }}
@else
{{ $crumbs->title() }}
@endif
@endforeach
@endif
```
And results in this output:
> [Home](#) / About
## Credits
For several years, I successfully used the [Dave James Miller](https://github.com/davejamesmiller/laravel-breadcrumbs) package to solve my problems, but he stopped developing and supporting it. After a long search for alternatives, I liked the [Dwight Watson](https://github.com/dwightwatson) 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.
## License
The MIT License (MIT). Please see [License File](LICENSE) for more information.