diglactic / laravel-breadcrumbs

Laravel Breadcrumbs - A simple Laravel-style way to create breadcrumbs.
https://packagist.org/packages/diglactic/laravel-breadcrumbs
MIT License
868 stars 63 forks source link

[Question] Is it possible to define the breadcrumbs NEXT to the routes themselves? #82

Closed fruitl00p closed 3 months ago

fruitl00p commented 3 months ago

Hi,

I'm probably running into something that others have aswell but I'd like to question it any just for clearification: I'd like to define the breadcrumb per named route but i'd also like to do it in the same file. This prevents issues with missing routes, parameters or other types of inconsistencies between the to. But it seems it's being loaded twice thus resulting in "Breadcrumb name "home" has already been registered" like errors:

// this is web.php file
// ... 
Route::get('/', HomeController::class)
    ->name('home');
Breadcrumbs::for('home', function (BreadcrumbTrail $trail) {
    $trail->push('Home', route('home'));
});

?>

Is there any way to avoid this but have to ability to keep them next to eachother?

shengslogar commented 3 months ago

Your routes file should not be loading twice. It's likely that Illuminate\Foundation\Support\Providers\RouteServiceProvider is somehow booting twice, which might be the result of a configuration problem in your app (e.g. are you extending RouteServiceProvider with your own class somewhere?).

Breadcrumbs do not belong in your routes file because they cannot be cached. Running php artisan route:cache will break your breadcrumbs. You should keep them separate.

fruitl00p commented 3 months ago

Sorry to bother you but adding a basic function in my routes/web.php doesnt error out with a 'function already defined' error, thus the file is actually only loaded once. (it was my false assumption based on the breadcrumbs error)

Thus I still don't understand why when adding the breadcrumbs call to my web.php file would give 'already defined' errors if I only define the breadcrumb once.

On your last note regarding the seperation of routes and breadcrumbs (which might solve it all anyway) That's a shame as again: I'd love to keep my routes defined and breadcrumbs to go with it as close to eachother as possible.

Thanks for your help and i'll reside to keeping my breadcrumbs separate from the routes :)

shengslogar commented 3 months ago

Happy to look into the error you're receiving if you can share a reproducible example. I was able to define breadcrumbs in my routes file earlier just fine.

fruitl00p commented 3 months ago

I found the duplication: I also updated the config to point to the web.php as where my breadcrumbs are defined: 'files' => base_path('routes/web.php'),

This then resulted in the double loading (i assume) Defaulting that back to a non-existing file (i.e base_path('routes/breadcrumbs.php') or NULL) solves the issue too and I can have my cake and eat it too ;) (keeping the routes and crumbs next to eachother)