Skip to content

Commit

Permalink
Added route detection
Browse files Browse the repository at this point in the history
  • Loading branch information
tabuna committed Aug 30, 2021
1 parent c5a6baa commit e00c717
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/Crumb.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tabuna\Breadcrumbs;

use Illuminate\Support\Facades\Route;
use JsonSerializable;

class Crumb implements JsonSerializable
Expand Down Expand Up @@ -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;
}
}
19 changes: 19 additions & 0 deletions tests/BreadcrumbsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
]);
}
}

0 comments on commit e00c717

Please sign in to comment.