Skip to content

Commit

Permalink
fix(laravel): handle route prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Feb 21, 2025
1 parent d2e1963 commit e1f5562
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/Laravel/Tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,13 @@ public function testDomainCanBeSet(): void
$response = $this->get('http://test.com/api/', ['accept' => ['application/ld+json']]);
$response->assertSuccessful();
}

public function testPrefixedOperations(): void
{
$response = $this->post('http://test.com/billing/calculate', [], ['content-type' => ['application/ld+json']]);
$response->assertSuccessful();

$response = $this->post('http://test.com/shipping/calculate', [], ['content-type' => ['application/ld+json']]);
$response->assertSuccessful();
}
}
7 changes: 4 additions & 3 deletions src/Laravel/routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;

$globalMiddlewares = config()->get('api-platform.routes.middleware', []);
$domain = config()->get('api-platform.routes.domain');
Expand All @@ -35,9 +34,11 @@
foreach ($resourceNameCollectionFactory->create() as $resourceClass) {
foreach ($resourceMetadataFactory->create($resourceClass) as $resourceMetadata) {
foreach ($resourceMetadata->getOperations() as $operation) {
$uriTemplate = str_replace('{._format}', '{_format?}', $operation->getUriTemplate());
$uriTemplate = rtrim($operation->getRoutePrefix(), '/').'/'.ltrim($uriTemplate, '/');

/* @var HttpOperation $operation */
Route::addRoute($operation->getMethod(), Str::replace('{._format}', '{_format?}', $operation->getUriTemplate()), ApiPlatformController::class)
->prefix($operation->getRoutePrefix())
Route::addRoute($operation->getMethod(), $uriTemplate, ApiPlatformController::class)
->middleware(ApiPlatformMiddleware::class.':'.$operation->getName())
->middleware($operation->getMiddleware())
->where('_format', '^\.[a-zA-Z]+')
Expand Down
29 changes: 29 additions & 0 deletions src/Laravel/workbench/app/Models/PrefixedOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Laravel\workbench\app\Models;

use ApiPlatform\Metadata\Post;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

#[Post(routePrefix: 'billing', uriTemplate: 'calculate', processor: [self::class, 'process'], status: 202)]
#[Post(routePrefix: 'shipping', uriTemplate: 'calculate', processor: [self::class, 'process'], status: 202)]
class PrefixedOperation extends Model
{
use HasFactory;

public static function process(): void
{
}
}
2 changes: 1 addition & 1 deletion src/OpenApi/Factory/OpenApiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
if ($this->routeCollection && $routeName && $route = $this->routeCollection->get($routeName)) {
$path = $route->getPath();
} else {
$path = ($operation->getRoutePrefix() ?? '').$operation->getUriTemplate();
$path = rtrim($operation->getRoutePrefix(), '/').'/'.ltrim($operation->getUriTemplate(), '/');
}

$path = $this->getPath($path);
Expand Down

0 comments on commit e1f5562

Please sign in to comment.