Gretel is a Laravel package for adding route-based breadcrumbs to your application.

Related tags

Laravel gretel
Overview

Gretel from the story 'Hansel and Gretel' holding bread behind her back

Gretel

Laravel breadcrumbs right out of a fairy tale.

Gretel is a Laravel package for adding route-based breadcrumbs to your application.

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');

Homepage Example

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');

Dashboard Example

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');

Nested Route Example

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);

Shallow Nested Example

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
Comments
  • Easily add breadcrumbs to routes of packages without overriding them

    Easily add breadcrumbs to routes of packages without overriding them

    Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] Current I am using Laravel Jetstream in my application, this package registers a lot of default routes - currently I haven't foùnd an easy way, except overriding the full route, to add breadcrumbs to these... Describe the solution you'd like A clear and concise description of what you want to happen.

    An easy way to add breadcrumbs to routes not in your control without the need of overiding them, something like:

    Gretel::breadcrumb('{ROUTE_NAME}', ....{NORMAL GRETEL PARAMS});
    // OR
    Gretel::breadcrumb('{ROUTE_URI}', ....{NORMAL GRETEL PARAMS});
    

    Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

    Overriding the full route to add the breadcrumb

    Additional context Add any other context or screenshots about the feature request here.

    enhancement 
    opened by ThaDaVos 6
  • Resource routing

    Resource routing

    Hi, I wanted to give a try to Your alternative library but I stopped on this:

    Route::resource('/products', ProductsController::class)->only('index', 'show');
    

    Is this handle resource routes?

    enhancement 
    opened by mgralikowski 5
  • Convenient way to export breadcrumbs as a collection

    Convenient way to export breadcrumbs as a collection

    I'm looking for a solution to handle breadcrumbs for an application written with Inertia. This package seems like a good candidate, I like the syntax macro'd to the routes.

    My issue is that this package was made with Blade in mind, thus it doesn't offer a convenient way to export the breadcrumbs as a collection. I need this feature because with Inertia, you pass whatever the front-end need to a middleware, and work with it in the front-end.

    My current workaround is the following:

    // Middleware/ShareNavigation.php
    public function handle($request, \Closure $next)
    {
        inertia()->share('navigation', [
            'breadcrumbs' => app(\Glhd\Gretel\View\Components\Breadcrumbs)->breadcrumbs,
            // other stuff
        ]);
    
        return $next($request);
    }
    

    A facade would be better:

    Breadcrumbs::render();
    // or something like
    Breadcrumbs::asCollection();
    
    enhancement 
    opened by innocenzi 5
  • Group parameters are missing

    Group parameters are missing

    Describe the bug By wrapping resource into a Route Group with {parameter}, throws an error that parameter doesn't exists,

    I've tried to pass the parameter as 3rd callback, but the same result.

    What version does this affect?

    • Laravel Version: [9.10.0]
    • Package Version: [1.5.0]

    To Reproduce

    Route::resource('movies', 'MovieController')->except([
        'show'
    ])->breadcrumbs(function($breadcrumbs) {
        $breadcrumbs
            ->index('Movies')
            ->create('Create')
            ->edit('Edit', '.index');
    });
    
    Route::prefix('/movies/{movie}')->group(function () {
    
        Route::resource('actors', 'ActorController')->except([
            'index', 'show'
        ])->breadcrumbs(function($breadcrumbs) {
            $breadcrumbs
                ->create('Create', 'movies.index', fn($movie) => $movie)
                ->edit('Edit', 'movies.index', fn($movie) => $movie);
        });
        
    });
    

    Expected behavior Generate breadcrumbs

    Additional context Throws an error:

    Missing required parameter for [Route: actors.create] [URI: movies/{movie}/actors/create] [Missing parameter: movie].

    bug 
    opened by Froxz 4
  • Breadcrumbs return an empty array for Resource routes

    Breadcrumbs return an empty array for Resource routes

    Describe the bug Using the breadcrumbs() method on a Resource route returns an empty array with either the Array or Callback syntax.

    What version does this affect?

    • Laravel Version: 8.73.2
    • Package Version: 1.4.0
    • Inertia.js Version: 0.4.5

    To Reproduce Here is an example of a resource route in my web.php routes file, although I've also tried the Callback syntax:

    Route::resource('/model, App\Http\Controllers\Web\Model\ModelController::class)
        ->names('web.models')
        ->breadcrumbs([
            'index' => 'Model',
            'create' => 'New Model',
            'show' => fn (\App\Models\Model $model) => $model->name,
            'edit' => 'Edit',
          ]);
    

    Expected behavior Dumping $request->route()->breadcrumbs()->jsonSerialize() should return an Array of the breadcrumbs; however, it returns an empty Array.

    Additional context

    bug 
    opened by MrThePlague 3
  • Recursive usage results in an infinity loop and crashes

    Recursive usage results in an infinity loop and crashes

    Describe the bug Recursive usage results in an infinity loop and crashes.

    What version does this affect?

    • Laravel Version: 8.0
    • Package Version: 1.6.0

    To Reproduce I'm using the breadcrumbs recursively to simulate the manner of a filesystem. Folders have subfolders, which have subfolders again and so on, e.g.: Home / Root / Folder / SubFolder / SubSubFolder ...

    My code is the following:

    Route::get('{folder?}', [FolderController::class, 'index'])
      ->name('folder.index')
      ->breadcrumb(
        fn(Folder $folder = null) => $folder ? $folder->label : 'Root', // The title of this breadcrumb
        fn(Folder $folder = null) => $folder ? 'folder.index' : 'home', // The parent breadcrumb
        fn(Folder $folder = null) => $folder?->parent, // The relation for the parent breadcrumb
      );
    

    But the application crashes with a 500 error due to an infinity loop. I believe it has to do with the third parameter. It should reset the $folder parameter in the route definition.

    Expected behavior If I open the "SubSubFolder", the recursive function should result in this structure: SubSubFolder => SubFolder => Folder => Root => Home

    bug 
    opened by raphaelbeckmann 1
  • Return type warning

    Return type warning

    Describe the bug When running with PHP 8.1, the following Warning is produced:

    LOG.warning: type of Glhd\Gretel\View\BreadcrumbIterator::current() should either be compatible with ArrayIterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in ~/project/vendor/glhd/gretel/src/View/BreadcrumbIterator.php on line 18
    

    What version does this affect?

    • Laravel Version: 9.6.0
    • Package Version: 1.5.0
    bug 
    opened by yakatz 1
  • Dash in a resource route name breaks Route Model binding

    Dash in a resource route name breaks Route Model binding

    Describe the bug When I have a resource route that has a dash in the name, route/model binding gets broken and an error "Missing required parameter for [Route....]"

    What version does this affect?

    • Laravel Version: 9.38
    • Package Version: 1.6.0

    To Reproduce Setup a simple Resource Controller using Resource Breadcrumbs. Ex: Route::resource('some-route', SomeRouteController::class)->breadcrumbs(function(ResourceBreadcrumbs $breadcrumbs) { $breadcrumbs->index('Index Rte')->show('Show Rte')->edit('Edit Rte'); }

    When trying to visit the Edit or Show routes and you will get the missing required parameter error. Changing the - to an underscore will correct the error. Separating the resource route into individual routes will also correct the error.

    Expected behavior I use dashes in my route names on a regular basis as they are easy to read and match up with Laravel's slug method

    bug 
    opened by butcherman 0
  • Create fake parent breadcrumbs

    Create fake parent breadcrumbs

    Is your feature request related to a problem? Please describe. I have routes that are 'nested', but the parent routes don't make any sense by themselves. For example, a user can get a text message that will take them to a special page, with the URL /sms/d/1234 (route name sms.dispatcher.call.show) or /sms/r/1234 (route name sms.responder.call.show). It wouldn't make any sense to have a route that was just /sms or /sms/d, but I would like to be able to prefix the breadcrumbs based on this route.

    Describe the solution you'd like

    Gretel::fakeBreadcrumb('sms', 'SMS');
    
    Route::name('dispatcher.')->prefix('d')->group(function (){
        Route::get('{notification:smsKey}', [CallController::class, 'showSms'])->name('call.show')->breadcrumb('Dispatch Call', 'sms');
    });
    
    

    This would produce a breadcrumb trail like SMS > Dispatch Call

    Describe alternatives you've considered The Gretel::breadcrumb() option requires that a route actually exist.

    Additional context Similar to https://github.com/diglactic/laravel-breadcrumbs#breadcrumbs-with-no-url

    enhancement 
    opened by yakatz 0
  • Add support for shallow nested resources

    Add support for shallow nested resources

    Hey! Just tried out this package and it seems great, but I run into a problem where shallow nested resources are not working correctly. I tried to fix this with limited amount of time and knowledge about this package, but seems like I got the tests to pass, so let me know if you think this is ok.

    Basically if we have something like this (same as in the test) and we visit notes/1/edit, then it wouldn't display anything at all, because in the registry it's registered as users.notes.edit and not notes.edit, but the route when it is resolved has name of notes.edit. So this change basically makes sure it is registered with shallow name.

    Route::resource('users', ResourceRoutesTestController::class)
        ->breadcrumbs([
            'index' => 'Users',
            'create' => 'New User',
            'edit' => 'Edit',
        ]);
    
      Route::resource('users.notes', NotesController::class)
        ->shallow()
        ->breadcrumbs(fn(ResourceBreadcrumbs $breadcrumbs) => $breadcrumbs
            ->show(fn(Note $note) => $note->note, 'users.index', fn(Note $note) => $note->user)
            ->edit('Edit', '.show', fn(Note $note) => $note->user)
        );
    

    Let me know what you think 👍

    opened by dsazup 0
Releases(1.6.0)
Owner
Galahad
Galahad
A simple laravel package to handle multiple key based model route binding

Laravel Model UUID A simple package to handle the multiple key/column based route model binding for laravel package Installation Require the package u

null 13 Mar 2, 2022
Stop duplicating your Eloquent query scopes and constraints in PHP. This package lets you re-use your query scopes and constraints by adding them as a subquery.

Laravel Eloquent Scope as Select Stop duplicating your Eloquent query scopes and constraints in PHP. This package lets you re-use your query scopes an

Protone Media 75 Dec 7, 2022
Quickly identify controller methods with no route in your Laravel applications.

Orphan Controller Quickly identify controller methods with no route in your Laravel applications. Installation You can install the package via Compose

Ryan Chandler 16 Feb 18, 2022
A Laravel package for quickly adding .well-known URLs

A Laravel package for quickly adding .well-known URLs well-known is a Laravel package for quickly adding well-known locations (RFC8615) to a Laravel a

Kim Hallberg 2 Sep 13, 2022
In Laravel, we commonly face the problem of adding repetitive filtering code, this package will address this problem.

Filterable In Laravel, we commonly face the problem of adding repetitive filtering code, sorting and search as well this package will address this pro

Zoran Shefot Bogoevski 1 Jun 21, 2022
A small package for adding UUIDs to Eloquent models.

A small package for adding UUIDs to Eloquent models. Installation You can install the package via composer: composer require ryangjchandler/laravel-uu

Ryan Chandler 40 Jun 5, 2022
Log requests and group together for aggregated statistics of route usage

Log Laravel route usage statistics Log Laravel requests and responses for statistical purposes and optionally aggregate by hours/days/months for minim

Bilfeldt 108 Dec 11, 2022
Simple laravel hook for adding meta tags to head for inertia

laravel seo hook for js frameworks simple hook for adding meta tags to <head></head> for js frameworks inertia:react,vue, etc... in app/Meta.php put M

Razmik Ayvazyan 2 Aug 23, 2022
Jetstrap is a lightweight laravel 8 package that focuses on the VIEW side of Jetstream / Breeze package installed in your Laravel application

A Laravel 8 package to easily switch TailwindCSS resources generated by Laravel Jetstream and Breeze to Bootstrap 4.

null 686 Dec 28, 2022
A Laravel chat package. You can use this package to create a chat/messaging Laravel application.

Chat Create a Chat application for your multiple Models Table of Contents Click to expand Introduction Installation Usage Adding the ability to partic

Tinashe Musonza 931 Dec 24, 2022
Load Laravel service providers based on your application's environment.

Laravel EnvProviders A more finetuned way of managing your service providers in Laravel. This package allows you to configure the environment certain

Sven Luijten 79 Dec 29, 2022
Laravel package to find performance bottlenecks in your laravel application.

Laravel Meter Laravel Meter monitors application performance for different things such as requests, commands, queries, events, etc and presents result

Sarfraz Ahmed 230 Dec 27, 2022
Laravel comments - This package enables to easily associate comments to any Eloquent model in your Laravel application

Laravel comments - This package enables to easily associate comments to any Eloquent model in your Laravel application

Rubik 4 May 12, 2022
A Laravel package helps you add a complete real-time messaging system to your new / existing application with only one command.

A Laravel package helps you add a complete real-time messaging system to your new / existing application with only one command.

Munaf Aqeel Mahdi 1.7k Jan 5, 2023
Laravel package that converts your application into a static HTML website

phpReel Static Laravel Package phpReel Static is a simple Laravel Package created and used by phpReel that converts your Laravel application to a stat

phpReel 16 Jul 7, 2022
A simple package allowing for consistent API responses throughout your Laravel application

Laravel API Response Helpers A simple package allowing for consistent API responses throughout your Laravel application. Requirements PHP ^7.4 | ^8.0

F9 Web Ltd. 441 Jan 5, 2023
A Laravel package to simplify using DPO Payment API in your application.

DPO (Direct Pay Online) Laravel Package The best DPO Laravel package, simple Ever This is the package that will help you add DPO Payment API to your L

Zepson Technologies 5 Nov 17, 2022
Smeify is a Stable Automated Solution for Airtime and Data businesses in Nigeria, this package helps you integrate smeify easily into your laravel application.

Smeify is a Stable Automated Solution for Airtime and Data businesses in Nigeria, this package helps you integrate smeify easily into your laravel application.

Ogundiran Adewale Charles 2 Jul 27, 2022
`dd` is a helper method in Laravel. This package will add the `dd` to your application.

dd dd is a helper method in Laravel. This package will add the dd to your application. Install Run composer require larapack/dd 1.* For Laravel Larave

Larapack 109 Dec 26, 2022