Skip to content

Commit b213164

Browse files
authored
fix(laravel): route where uses requirements (#7199)
fixes #7194
1 parent 53577fd commit b213164

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

src/Laravel/Tests/ValidationTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,12 @@ public function testValidationSnakeCase(): void
5757
$response = $this->postJson('/api/issue_6932', $data, ['accept' => 'application/ld+json', 'content-type' => 'application/ld+json']);
5858
$response->assertStatus(422);
5959
}
60+
61+
public function testRouteWithRequirements(): void
62+
{
63+
$response = $this->get('api/issue_7194_requirements/test', ['accept' => 'application/ld+json']);
64+
$response->assertStatus(404);
65+
$response = $this->get('api/issue_7194_requirements/1', ['accept' => 'application/ld+json']);
66+
$response->assertStatus(200);
67+
}
6068
}

src/Laravel/routes/api.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@
4747

4848
/* @var HttpOperation $operation */
4949
$route = Route::addRoute($operation->getMethod(), $uriTemplate, ['uses' => ApiPlatformController::class, 'prefix' => $operation->getRoutePrefix() ?? ''])
50-
->where('_format', '^\.[a-zA-Z]+')
50+
->where([
51+
'_format' => '^\.[a-zA-Z]+',
52+
] + ($operation->getRequirements() ?? []))
5153
->name($operation->getName())
5254
->setDefaults(['_api_operation_name' => $operation->getName(), '_api_resource_class' => $operation->getClass()]);
5355

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Workbench\App\ApiResource;
15+
16+
use ApiPlatform\Metadata\Get;
17+
use ApiPlatform\Metadata\Operation;
18+
19+
#[Get(
20+
uriTemplate: '/issue_7194_requirements/{key}',
21+
uriVariables: ['key'],
22+
requirements: ['key' => '\d+'],
23+
provider: [self::class, 'provide']
24+
)]
25+
class UriRequirements
26+
{
27+
public function __construct(
28+
public int $key,
29+
) {
30+
}
31+
32+
public static function provide(Operation $operation, array $uriVariables = []): self
33+
{
34+
return new self((int) $uriVariables['key']);
35+
}
36+
}

0 commit comments

Comments
 (0)