Skip to content

Commit aa8a5bb

Browse files
Exclude cached routes with randomly generated names (#370)
* Add failing test * Filter out cached generated route names * Remove unused variable
1 parent 38460b4 commit aa8a5bb

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

src/Ziggy.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ public function filter($filters = [], $include = true): self
8787
private function nameKeyedRoutes()
8888
{
8989
[$fallbacks, $routes] = collect(app('router')->getRoutes()->getRoutesByName())
90+
->reject(function ($route) {
91+
return Str::startsWith($route->getName(), 'generated::');
92+
})
9093
->partition(function ($route) {
9194
return $route->isFallback;
9295
});

tests/Unit/RouteCachingTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
use Tests\TestCase;
6+
use Tightenco\Ziggy\Ziggy;
7+
8+
class RouteCachingTest extends TestCase
9+
{
10+
/** @test */
11+
public function can_exclude_routes_with_randomly_generated_names()
12+
{
13+
app('router')->get('users', $this->noop())->name('users');
14+
app('router')->get('cached', $this->noop())->name('generated::ZRopaJJwzA27wRLa');
15+
16+
$expected = [
17+
'users' => [
18+
'uri' => 'users',
19+
'methods' => ['GET', 'HEAD'],
20+
],
21+
];
22+
23+
$this->assertSame($expected, (new Ziggy)->toArray()['routes']);
24+
}
25+
}

tests/Unit/ZiggyTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,6 @@ public function can_include_only_middleware_set_in_config()
336336
/** @test */
337337
public function can_order_fallback_routes_last()
338338
{
339-
$ziggy = new Ziggy;
340339
app('router')->fallback($this->noop())->name('fallback');
341340
app('router')->get('/users', $this->noop())->name('users.index');
342341

0 commit comments

Comments
 (0)