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

Define action instead of route name #56

Closed antonkomarev closed 2 years ago

antonkomarev commented 2 years ago

Can we link breadcrumb trails to actions, not routes?

Instead of:

Breadcrumbs::for('article-list', function (BreadcrumbTrail $trail) {
    $trail->parent('home');
    $trail->push(
        'Articles',
        route('article-list'),
    );
});

Write:

Breadcrumbs::for('article-list', function (BreadcrumbTrail $trail) {
    $trail->parent('home');
    $trail->push(
        'Articles',
        action(\App\Http\Web\RenderArticleListController::class),
    );
});

With routes we are losing the way to understand from the IDE where this class was used and how change could affect on the application.

antonkomarev commented 2 years ago

Right now we are getting exception:

The current route (GET /articles) is not named

And if 'unnamed-route-exception' => false, then breadcrumbs are just not rendering.

shengslogar commented 2 years ago

Actions can ingest variables. https://laravel.com/docs/9.x/helpers#urls

route and action both return strings. Breadcrumbs doesn't know the difference. So $trail->push('Foo', 'http://bar') will work.

If you need to extend this further, consider writing a macro.

Right now we are getting exception:

The current route (GET /articles) is not named

Breadcrumbs operates entirely under the premise of having named parent and child routes. If you don't want to name your routes, this library isn't a good fit for you.