Skip to content

[Laravel] Route prefixes conflict when multiple resources share same URI pattern #6969

@JeremUndefined

Description

@JeremUndefined

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

  1. 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 {}

  1. Run php artisan route:list

  2. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions