web-id / breadcrumb
Make breadcrumbs using a route macro
Installs: 11 856
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 4
Forks: 0
Open Issues: 3
Requires
- php: ^8.2|^8.3
- illuminate/contracts: ^10.0|^11.0
- inertiajs/inertia-laravel: ^0.6.0|^1.0
- spatie/laravel-navigation: ^1.1|^1.2
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.0
- laravel/pint: ^1.0
- nunomaduro/collision: ^8.0
- orchestra/testbench: ^9.0
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^11.0
- dev-main
- v1.2.4.x-dev
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.2
- 1.0.1
- 1.0.0
- dev-add-validation-and-tests
- dev-php8.2
- dev-laravel_10
- dev-dependabot/github_actions/aglipanci/laravel-pint-action-2.2.0
- dev-dependabot/github_actions/ramsey/composer-install-2
- dev-bugfix_inertia_config
- dev-issue1/add-ide-helper
- dev-add-blade-support
This package is auto-updated.
Last update: 2024-10-13 10:36:55 UTC
README
Installation
You can install the package via composer:
composer require web-id/breadcrumb
Publish config file
php artisan vendor:publish --provider="WebId\Breadcrumb\BreadcrumbServiceProvider" --tag="config"
Usage
This package uses Spatie's Laravel Navigation package (https://github.com/spatie/laravel-navigation).
Frontend
If you are using Inertia, update the frontend
key to the config file to inertia
.
'frontend' => 'inertia',
If your project uses Blade templates, set the config to blade
.
'frontend' => 'blade',
Breadcrumb root element
Edit the breadcrumb_root
title and route name in your published breadcrumb.php
config file.
For instance, if your root element is your homepage:
'breadcrumb_root' => [ 'title' => 'Homepage', 'route_name' => 'homepage', ],
Note
Your route must be named.
Breadcrumb class
To create a new breadcrumb, create a class that extends the WebId\Breadcrumb\Breadcrumb
class.
In this example, we will create a breadcrumb for the blog page (list of all articles):
use WebId\Breadcrumb\Breadcrumb; class BlogBreadcrumb extends Breadcrumb { public function index(): array { return $this->render( $this->baseBreadcrumb() ->add('Blog') ->tree() ); } }
Notice that the add()
method only takes a name (blog). Indeed, the last element of a breadcrumb being
the active page, you don't need to attach a link to it.
In this example, your view should have a breadcrumb
key with this data:
[ { "url": "https://www.yourwebsite.com", "title": "Homepage" }, { "title": "Blog" } ]
If you want to create the breadcrumb for a single blog post, you probably want the parent element to be the blog page. In that case, add a method as follow:
public function show(Post $post): array { return $this->render( $this->baseBreadcrumb() ->add('Blog', route('blog.index')) ->add($post->title, route('blog.show', ['post' => $post])) ->tree() ); }
Notice that your can Typehint a model in your breadcrumb methods as if you were in a controller method.
In this example, your view should have a breadcrumb
key with this data:
[ { "url": "https://www.yourwebsite.com", "title": "Homepage" }, { "url": "https://www.yourwebsite.com/blog", "title": "Blog" }, { "title": "Article title" } ]
Register a breadcrumb to a route
In order for your breadcrumb to be accessible in your Inertia view, you have to register it to the associated route as follow:
Route::get('/blog', [BlogController::class, 'index'])->breadcrumb([BlogBreadcrumb:class, 'index']); Route::get('/blog/{post}', [BlogController::class, 'show'])->breadcrumb([BlogBreadcrumb:class, 'show']);
Testing
composer test
License
The MIT License (MIT). Please see License File for more information.