diff --git a/src/Laravel/ApiPlatformProvider.php b/src/Laravel/ApiPlatformProvider.php index d30598bfe53..8566cbc32f0 100644 --- a/src/Laravel/ApiPlatformProvider.php +++ b/src/Laravel/ApiPlatformProvider.php @@ -1342,6 +1342,7 @@ public function boot(ResourceNameCollectionFactoryInterface $resourceNameCollect // _format is read by the middleware $uriTemplate = $operation->getRoutePrefix().str_replace('{._format}', '{_format?}', $uriTemplate); $route = (new Route([$operation->getMethod()], $uriTemplate, [ApiPlatformController::class, '__invoke'])) + ->where('_format', '^\.[a-zA-Z]+') ->name($operation->getName()) ->setDefaults(['_api_operation_name' => $operation->getName(), '_api_resource_class' => $operation->getClass()]); diff --git a/src/Laravel/Tests/JsonLdTest.php b/src/Laravel/Tests/JsonLdTest.php index 46039ebe68e..46e7c806334 100644 --- a/src/Laravel/Tests/JsonLdTest.php +++ b/src/Laravel/Tests/JsonLdTest.php @@ -41,6 +41,7 @@ protected function defineEnvironment($app): void { tap($app['config'], function (Repository $config): void { $config->set('app.debug', true); + $config->set('api-platform.resources', [app_path('Models'), app_path('ApiResource')]); }); } @@ -337,4 +338,14 @@ public function testRelationWithGroups(): void $this->assertArrayHasKey('relation', $content); $this->assertArrayHasKey('name', $content['relation']); } + + /** + * @see https://github.com/api-platform/core/issues/6779 + */ + public function testSimilarRoutesWithFormat(): void + { + $response = $this->get('/api/staff_position_histories?page=1', ['accept' => 'application/ld+json']); + $response->assertStatus(200); + $this->assertSame('/api/staff_position_histories', $response->json()['@id']); + } } diff --git a/src/Laravel/workbench/app/ApiResource/Staff.php b/src/Laravel/workbench/app/ApiResource/Staff.php new file mode 100644 index 00000000000..5376f1863da --- /dev/null +++ b/src/Laravel/workbench/app/ApiResource/Staff.php @@ -0,0 +1,25 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Workbench\App\ApiResource; + +use ApiPlatform\Metadata\ApiResource; + +#[ApiResource(provider: [self::class, 'provide'])] +class Staff +{ + public static function provide() + { + return []; + } +} diff --git a/src/Laravel/workbench/app/ApiResource/StaffPositionHistory.php b/src/Laravel/workbench/app/ApiResource/StaffPositionHistory.php new file mode 100644 index 00000000000..df0f243a253 --- /dev/null +++ b/src/Laravel/workbench/app/ApiResource/StaffPositionHistory.php @@ -0,0 +1,25 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Workbench\App\ApiResource; + +use ApiPlatform\Metadata\ApiResource; + +#[ApiResource(provider: [self::class, 'provide'])] +class StaffPositionHistory +{ + public static function provide() + { + return []; + } +}