Skip to content

Commit a16147a

Browse files
authored
fix(laravel): handle route prefix (#6978)
fixes #6969
1 parent d2e1963 commit a16147a

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

src/Laravel/Tests/ApiTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,13 @@ public function testDomainCanBeSet(): void
4545
$response = $this->get('http://test.com/api/', ['accept' => ['application/ld+json']]);
4646
$response->assertSuccessful();
4747
}
48+
49+
public function testPrefixedOperations(): void
50+
{
51+
$response = $this->post('http://test.com/billing/calculate', [], ['content-type' => ['application/ld+json']]);
52+
$response->assertSuccessful();
53+
54+
$response = $this->post('http://test.com/shipping/calculate', [], ['content-type' => ['application/ld+json']]);
55+
$response->assertSuccessful();
56+
}
4857
}

src/Laravel/routes/api.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
2424
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
2525
use Illuminate\Support\Facades\Route;
26-
use Illuminate\Support\Str;
2726

2827
$globalMiddlewares = config()->get('api-platform.routes.middleware', []);
2928
$domain = config()->get('api-platform.routes.domain');
@@ -35,9 +34,11 @@
3534
foreach ($resourceNameCollectionFactory->create() as $resourceClass) {
3635
foreach ($resourceMetadataFactory->create($resourceClass) as $resourceMetadata) {
3736
foreach ($resourceMetadata->getOperations() as $operation) {
37+
$uriTemplate = str_replace('{._format}', '{_format?}', $operation->getUriTemplate());
38+
$uriTemplate = rtrim($operation->getRoutePrefix(), '/').'/'.ltrim($uriTemplate, '/');
39+
3840
/* @var HttpOperation $operation */
39-
Route::addRoute($operation->getMethod(), Str::replace('{._format}', '{_format?}', $operation->getUriTemplate()), ApiPlatformController::class)
40-
->prefix($operation->getRoutePrefix())
41+
Route::addRoute($operation->getMethod(), $uriTemplate, ApiPlatformController::class)
4142
->middleware(ApiPlatformMiddleware::class.':'.$operation->getName())
4243
->middleware($operation->getMiddleware())
4344
->where('_format', '^\.[a-zA-Z]+')
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
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 ApiPlatform\Laravel\workbench\app\Models;
15+
16+
use ApiPlatform\Metadata\Post;
17+
use Illuminate\Database\Eloquent\Factories\HasFactory;
18+
use Illuminate\Database\Eloquent\Model;
19+
20+
#[Post(routePrefix: 'billing', uriTemplate: 'calculate', processor: [self::class, 'process'], status: 202)]
21+
#[Post(routePrefix: 'shipping', uriTemplate: 'calculate', processor: [self::class, 'process'], status: 202)]
22+
class PrefixedOperation extends Model
23+
{
24+
use HasFactory;
25+
26+
public static function process(): void
27+
{
28+
}
29+
}

src/OpenApi/Factory/OpenApiFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
169169
if ($this->routeCollection && $routeName && $route = $this->routeCollection->get($routeName)) {
170170
$path = $route->getPath();
171171
} else {
172-
$path = ($operation->getRoutePrefix() ?? '').$operation->getUriTemplate();
172+
$path = rtrim($operation->getRoutePrefix() ?? '', '/').'/'.ltrim($operation->getUriTemplate(), '/');
173173
}
174174

175175
$path = $this->getPath($path);

0 commit comments

Comments
 (0)