-
- Notifications
You must be signed in to change notification settings - Fork 932
Description
API Platform version(s) affected: 4.0.17
Description
In API Platform Laravel 4.0.17, there is an issue with route prefix handling that causes route conflicts. When multiple resources define routes with the same URI pattern but different prefixes, only the last registered route remains accessible while others are silently overwritten.
For example, if you have two resources with /calculate endpoint but different prefixes (/billing and /shipping), only one of them will be accessible (the last one registered).
How to reproduce
- Create two API resources with the same endpoint but different prefixes:
#[ApiResource( operations: [ new Post('/calculate') ], routePrefix: '/billing' )] class PriceCalculator {}
#[ApiResource( operations: [ new Post('/calculate') ], routePrefix: '/shipping' )] class ShippingCalculator {}
-
Run
php artisan route:list
-
Only /shipping/calculate appears in the route list, /billing/calculate is missing
Possible Solution
The issue can be fixed by modifying the route registration in vendor/api-platform/laravel/routes/api.php
. Instead of applying the prefix separately, we should combine it with the URI template before route registration:
$prefix = $operation->getRoutePrefix();
$uri = $operation->getUriTemplate();
$fullUri = trim($prefix . '/' . ltrim($uri, '/'), '/');
Route::addRoute( $operation->getMethod(), $fullUri, ApiPlatformController::class )->middleware(...)
Additional Context
The issue appears to be introduced in version 4.0.17
The same code works correctly in version 4.0.16