Gretel
Laravel breadcrumbs right out of a fairy tale.
Gretel is a Laravel package for adding route-based breadcrumbs to your application.
- Defining Breadcrumbs
- Displaying Breadcrumbs
- Using Gretel With Your CSS Framework of Choice
- Using a Custom Template (while maintaining accessibility)
- Caching Breadcrumbs (required if using
route:cache
) - Handling Errors
Installation
composer require glhd/gretel
Usage
Defining Breadcrumbs
Gretel adds a new Route macro that you can use when defining your routes:
Single Breadcrumb
In the simplest case, chain the breadcrumb()
function onto your existing route to define a breadcrumb:
Route::get('/', HomeController::class)
->name('home')
->breadcrumb('Home');
If you need to dynamically control the title, pass in a closure instead:
Route::get('/dashboard', DashboardController::class)
->name('dashboard')
->breadcrumb(fn() => Auth::user()->name.'’s dashboard');
Nested Breadcrumb
Breadcrumbs aren't very useful unless you string them together. Gretel handles nested breadcrumbs by pointing to a previously-defined parent breadcrumb:
Route::get('/users', [UserController::class, 'index'])
->name('users.index')
->breadcrumb('Users');
Route::get('/users/{user}', [UserController::class, 'show'])
->name('users.show')
->breadcrumb(fn(User $user) => $user->name, 'users.index');
Route::get('/users/{user}/edit', [UserController::class, 'edit'])
->name('users.edit')
->breadcrumb('Edit', 'users.show');
Here, you can see that our users.show
route references users.index
as its parent. This way, when you render breadcrumbs for users.show
it will also show the breadcrumb for users.index
.
Gretel assumes that the parameters in nested routes can be safely used for their parent routes. In this example, users.edit
will render the users.show
breadcrumb using the User
value that was resolved for the edit action. In the vast majority of cases, this is exactly what you want. If not, you can override this behavior (see below).
Parent Shorthand
Often, a child route will reference a parent with the same name prefix. In our above example, users.show
references users.index
and users.edit
references users.show
. In this case, you can use the parent shorthand:
Route::get('/admin/users/{user}/notes/create', [NotesController::class, 'create'])
->name('admin.users.notes.create')
->breadcrumb('Add Note', '.index'); // shorthand for "admin.users.notes.index"
This is particularly useful for large apps that have many deeply nested routes.
Shallow Nested Routes
If your nested routes do not contain the route parameters necessary for the parent route, you will need to provide the values to Gretel. You can do this using a third callback:
Route::get('/companies/{company}', [CompanyController::class, 'show'])
->name('companies.show')
->breadcrumb(fn(Company $company) => $company->name);
Route::get('/users/{user}', [UserController::class, 'show'])
->name('users.show')
->breadcrumb(fn(User $user) => $user->name, 'companies.show', fn(User $user) => $user->company);
Displaying Breadcrumbs
You can display the breadcrumbs for the current route with the
Blade component. The Blade component accepts a few optional attributes:
Attribute | |
---|---|
framework |
Render to match a UI framework ("tailwind" by default) |
view |
Render a custom view (supersedes the framework attribute) |
jsonld |
Render as a JSON-LD tag |
Supported Frameworks
Gretel supports most common CSS frameworks. We've taken the CSS framework's documented markup and added additional aria-
tags where appropriate for better accessibility. Currently supported frameworks:
Tailwind use "tailwind"
(default)
Materialize use "materialize"
Bootstrap 5 use "bootstrap5"
Bulma use "bulma"
Semantic UI use "semantic-ui"
Primer use "primer"
Foundation 6 use "foundation6"
UIKit use "uikit"
Older Frameworks
Older versions of some frameworks are also available:
- Bootstrap 3 use
"bootstrap3"
- Bootstrap 4 use
"bootstrap4"
- Foundation 5 use
"foundation5"
You'll typically want to include the
tag somewhere in your application layout (maybe twice if you're using JSON-LD):