Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(laravel): Prevent overwriting existing routes on the router #6941

Merged
merged 4 commits into from
Feb 7, 2025

Conversation

jonerickson
Copy link
Contributor

@jonerickson jonerickson commented Feb 5, 2025

Problem

When the API Platform service provider is not auto-discovered, and manually added as a provider, the SP would overwrite any route that had been registered up until that point.

Because of the Eloquent ORM introspection that happens when discovering metadata, a DB service is required to perform the introspection. This happens whenever composer php artisan package:discover is run. This is not optimal for CI/CD environments where you often don't have those services running to perform chores like linting and static analysis.

Solution

By allowing the developer to add API Platforms SP to composer's dont-discover array and manually registering the SP at a later time, we can prevent the needs of the DB specifically when php artisan package:discover is run. We then register API Platform's SP in our providers array and let Laravel do the rest. Because the SP is now being registered AFTER the default RouteServiceProvider, this PR will no longer overwrite the existing routes as it did before.

@jonerickson jonerickson marked this pull request as draft February 5, 2025 07:39
@jonerickson
Copy link
Contributor Author

jonerickson commented Feb 5, 2025

I think the best way to go is to define the routes in a separate routes file like most packages and load them in using the SP helpers. This will take care of determining if they are cached yet etc and will cache them if called by the user.

https://laravel.com/docs/11.x/packages#routes

TLDR; I don't think it's good practice to completely reset all the routes on the router as it does now but allow them to me merged naturally by the framework.

Copy link
Member

@soyuka soyuka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this it indeed looks better to load routes in a separated file, I'm just wondering why the 404 test is broken. To run tests you can follow instructions at https://github.com/api-platform/core/blob/main/src/Laravel/CONTRIBUTING.md#tests.

Also, please run our cs fixer as it looks like your IDE has a different ruleset then ours which makes the diff larger then it should.

Thanks very much for the patch!

src/Laravel/ApiPlatformProvider.php Outdated Show resolved Hide resolved
src/Laravel/ApiPlatformProvider.php Outdated Show resolved Hide resolved
src/Laravel/ApiPlatformProvider.php Outdated Show resolved Hide resolved
src/Laravel/ApiPlatformProvider.php Outdated Show resolved Hide resolved
src/Laravel/ApiPlatformProvider.php Outdated Show resolved Hide resolved
src/Laravel/ApiPlatformProvider.php Outdated Show resolved Hide resolved
@jonerickson
Copy link
Contributor Author

jonerickson commented Feb 6, 2025

I like this it indeed looks better to load routes in a separated file, I'm just wondering why the 404 test is broken. To run tests you can follow instructions at https://github.com/api-platform/core/blob/main/src/Laravel/CONTRIBUTING.md#tests.

Also, please run our cs fixer as it looks like your IDE has a different ruleset then ours which makes the diff larger then it should.

Thanks very much for the patch!

Thanks for the reply! Testing this out in an actual prod environment and I am liking the way it's working. Adding the ability to specify a domain is paramount for many apps.

The 404 is because I forgot to add the index where constraint to the route. Will update.

I will make sure to lint and format before marking this ready for review.

@jonerickson jonerickson requested a review from soyuka February 7, 2025 06:12
@jonerickson jonerickson marked this pull request as ready for review February 7, 2025 06:12
@jonerickson
Copy link
Contributor Author

@soyuka I believe this is ready for review.

commitlint is still failing but I believe this is because I opened the PR with the incorrect title. Added the missing where constraint to the /{index?}{_format?} route which fixed the failing test.

@soyuka
Copy link
Member

soyuka commented Feb 7, 2025

don't worry about commitlint, it takes the first commit of the patch not the PR title but I'll squash anyways and it won't be an issue, awesome patch many thanks @jonerickson !

@soyuka soyuka merged commit 5124d8c into api-platform:4.0 Feb 7, 2025
60 of 61 checks passed
Route::group(['prefix' => $prefix], function (): void {
Route::group(['middleware' => ApiPlatformMiddleware::class], function (): void {
Route::get('/contexts/{shortName?}{_format?}', ContextAction::class)
->middleware(ApiPlatformMiddleware::class)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I safely remove this call as we're in a Route::group call that already has the middleware?

Copy link
Contributor Author

@jonerickson jonerickson Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, as well as the 3 below it as well. thanks for catching that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done at 7007dcc

@jonerickson jonerickson deleted the feature/router-fix branch February 7, 2025 16:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants