Skip to content

Commit

Permalink
Merge pull request #658 from tighten/route-name-numbers
Browse files Browse the repository at this point in the history
Fix numeric route names
  • Loading branch information
bakerkretzmar authored Aug 4, 2023
2 parents abe40e6 + b327019 commit 967074f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/Ziggy.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,22 @@ private function nameKeyedRoutes()

$bindings = $this->resolveBindings($routes->toArray());

return $routes->merge($fallbacks)
->map(function ($route) use ($bindings) {
return collect($route)->only(['uri', 'methods', 'wheres'])
->put('domain', $route->domain())
->put('bindings', $bindings[$route->getName()] ?? [])
->when($middleware = config('ziggy.middleware'), function ($collection) use ($middleware, $route) {
if (is_array($middleware)) {
return $collection->put('middleware', collect($route->middleware())->intersect($middleware)->values()->all());
}

return $collection->put('middleware', $route->middleware());
})->filter();
});
$fallbacks->map(function ($route, $name) use ($routes) {
$routes->put($name, $route);
});

return $routes->map(function ($route) use ($bindings) {
return collect($route)->only(['uri', 'methods', 'wheres'])
->put('domain', $route->domain())
->put('bindings', $bindings[$route->getName()] ?? [])
->when($middleware = config('ziggy.middleware'), function ($collection) use ($middleware, $route) {
if (is_array($middleware)) {
return $collection->put('middleware', collect($route->middleware())->intersect($middleware)->values()->all());
}

return $collection->put('middleware', $route->middleware());
})->filter();
});
}

/**
Expand Down
34 changes: 34 additions & 0 deletions tests/Unit/ZiggyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -652,4 +652,38 @@ public function filter_route_names_from_nested_groups()
$this->assertArrayNotHasKey('foo.bar', (new Ziggy)->toArray()['routes']);
$this->assertArrayNotHasKey('foo.bar.baz', (new Ziggy)->toArray()['routes']);
}

/** @test */
public function numeric_route_names()
{
app('router')->get('a', $this->noop())->name('a');
app('router')->get('3', $this->noop())->name('3');
app('router')->get('b', $this->noop())->name('b');
app('router')->fallback($this->noop())->name('404');
app('router')->getRoutes()->refreshNameLookups();

config(['ziggy.except' => ['home', 'posts.*', 'postComments.*', 'admin.*']]);

$this->assertSame([
'a' => [
'uri' => 'a',
'methods' => ['GET', 'HEAD'],
],
3 => [
'uri' => '3',
'methods' => ['GET', 'HEAD'],
],
'b' => [
'uri' => 'b',
'methods' => ['GET', 'HEAD'],
],
404 => [
'uri' => '{fallbackPlaceholder}',
'methods' => ['GET', 'HEAD'],
'wheres' => [
'fallbackPlaceholder' => '.*',
],
],
], (new Ziggy)->toArray()['routes']);
}
}

0 comments on commit 967074f

Please # to comment.