Skip to content

Commit

Permalink
Fix match no_version. in route names
Browse files Browse the repository at this point in the history
  • Loading branch information
reindert-vetter committed Jan 12, 2022
1 parent a0f55f8 commit 6a6c4d2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ Route::middleware(['api', ApiVersionControl::class])
->where(['version' => 'v\d{1,3}'])
->group(base_path('routes/api.php'));
```
_You can see here that we prefix the route name with `default.` (for the routers without a version). You have to do that to avoid the error `Another route is already using that name` when caching the routers. Decide for yourself whether this is desirable for your application._
_You can see here that we prefix the route name with `no_version.` (for the routers without a version). You have to do that to avoid the error `Another route is already using that name` when caching the routers. Decide for yourself whether this is desirable for your application._

3. Add `\ReindertVetter\ApiVersionControl\ApiVersionControlServiceProvider::class` to your providers in config/app.php
4. Create a config file by running `php artisan vendor:publish --provider='ReindertVetter\ApiVersionControl\ApiVersionControlServiceProvider'`.
Expand Down
3 changes: 2 additions & 1 deletion src/Helper/RouteNameMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public function match(string $key): bool
{
$names = explode('|', $key);
foreach ($names as $name) {
if ($this->request->route()->getName() === $name) {
$routeName = preg_replace('/^no_version\./', '', $this->request->route()->getName());
if ($routeName === $name) {
return true;
}
}
Expand Down
25 changes: 25 additions & 0 deletions test/Unit/MatchReleases/RouteNameMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,29 @@ public function testWithMultipleNames(): void

$this->assertEquals(1, $collection->count());
}

public function testNoVersionPrefix(): void
{
$config = [
'releases' => [
'orders.index|orders.show' => [
'<=2' => [
ExamplePrepareParameterException::class,
],
],
'default' => [],
],
'route_matcher' => RouteNameMatcher::class,
];

$request = new Request();
$request->server->set('REQUEST_URI', '/v2/orders');
$request->setRouteResolver(function () use ($request) {
return (new Route('GET', 'the_route', []))->bind($request)->name('no_version.orders.index');
});

$collection = MiddlewareCollection::createFromConfig($request, $config);

$this->assertEquals(1, $collection->count());
}
}

0 comments on commit 6a6c4d2

Please sign in to comment.