-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
[8.x] Enforce implicit Route Model scoping #39440
Conversation
Actually, you don't need to give the model route key in order to scope the post to the given user You can just define your parameter with an extra Route::get('/users/{user}/posts/{post:}', function (User $user, Post $post) {
// $post belongs to $user and route parameter is the default one
return $post;
}); Maybe it just deserves to be documented ;-) |
Not sure if trolling, @bastien-phi That's how it works right now, which is documented. You are providing a key, but because that key is an empty string, it falls back to the Eloquent model key (likely "id") and uses that instead. Furthermore, this still feels like a hacky way to enable this feature's behaviour. This PR enables the possibility to enable it application wide by default using a feature flag. |
I was just saying that passing
Indeed, not possible for now. Good job ! |
Made a few tweaks here. You can now do: Route::get('/posts/{post}/comments/{comment}', function (Post $post, Comment $comment) {
return $comment;
})->scopeBindings(); Or: Route::scopeBindings()->group(function () {
Route::get('/posts/{post}/comments/{comment}', function (Post $post, Comment $comment) {
return $comment;
});
}); |
This PR implements the functionality requested in #32144.
With this PR, any Route (or route group) with the
scoping
action set totrue
will tell the framework to scope the second Eloquent model as such that it must be a child of the previous Eloquent model in the same route, without the need to provide a slug.This is useful when:
id
slug everywhere just to enable this behaviour(
Route::get('/users/{user}/posts/{post:id}', function (User ...
)While by itself this is only a somewhat useful feature, I think it would be quite useful when also providing the ability to enable it application-wide using a class-level property in the
RouteServiceProvider
, essentially making it very easy to either opt-in or opt-out, and to what extent: