From e00c7175f804b12c4765cdd341327d1ed967522c Mon Sep 17 00:00:00 2001 From: tabuna Date: Mon, 30 Aug 2021 22:31:05 +0300 Subject: [PATCH] Added route detection --- README.md | 22 ++++++++++++++++++++++ src/Crumb.php | 3 ++- tests/BreadcrumbsTest.php | 19 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 259e9ae..8dca221 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ Run this at the command line: ```php $ composer require tabuna/breadcrumbs ``` + This will update `composer.json` and install the package into the `vendor/` directory. ## Define your breadcrumbs @@ -63,6 +64,27 @@ Route::get('/category/{category}', function (Category $category){ ); ``` + +## Route detection + +The package tries to reduce the number of lines needed. For this, you can skip passing the results of the `route()` methods. +The following two declarations will be equivalent: + +```php +Route::get('/', fn () => view('home')) + ->name('home') + ->breadcrumbs(fn (Trail $trail) => + $trail->push('Home', route('home')) +); + +Route::get('/', fn () => view('home')) + ->name('home') + ->breadcrumbs(fn (Trail $trail) => + $trail->push('Home', 'home') +); +``` + + ## Like to use a separate route file? You can do this simply by adding the desired file to the service provider diff --git a/src/Crumb.php b/src/Crumb.php index b968b2a..2cb2fff 100644 --- a/src/Crumb.php +++ b/src/Crumb.php @@ -4,6 +4,7 @@ namespace Tabuna\Breadcrumbs; +use Illuminate\Support\Facades\Route; use JsonSerializable; class Crumb implements JsonSerializable @@ -63,6 +64,6 @@ public function title(): string */ public function url(): ?string { - return $this->url; + return Route::has($this->url) ? route($this->url) : $this->url; } } diff --git a/tests/BreadcrumbsTest.php b/tests/BreadcrumbsTest.php index a0018c5..7f4998b 100644 --- a/tests/BreadcrumbsTest.php +++ b/tests/BreadcrumbsTest.php @@ -276,4 +276,23 @@ public function testBreadcrumbsIdempotency(): void ], ]); } + + public function testBreadcrumbsShortRoute(): void + { + Route::get('breadcrumbs-about-test', function () { + return Breadcrumbs::current()->toJson(); + }) + ->name('breadcrumbs.about') + ->breadcrumbs(function (Trail $trail) { + return $trail->push('About', 'breadcrumbs.about'); + }); + + $this->get('breadcrumbs-about-test') + ->assertJson([ + [ + 'title' => 'About', + 'url' => 'http://localhost/breadcrumbs-about-test', + ], + ]); + } }