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

[7.x] Scoped resource routes #33752

Merged
merged 5 commits into from
Aug 10, 2020
Merged

[7.x] Scoped resource routes #33752

merged 5 commits into from
Aug 10, 2020

Conversation

iamgergo
Copy link
Contributor

@iamgergo iamgergo commented Aug 4, 2020

This PR adds the ability to make resource routes scoped.

For a while, we have the ability to scope routes when using Implicit Route Model Binding. This is very useful because it provides a quick and easy route key customization and respects the parent-child relations as well:

Route::get('api/users/{user}/posts/{post:slug}', function (User $user, Post $post) {
    return $post;
});

This PR implements the same, but for resource routes, so if we wish to use the same functionality for a resource URL structure, we don't need to define the routes one-by-one to use this feature or writing a custom route binding resolution logic for each model.


Usage:

// Using the default route keys
Route::resource('users.tasks', TasksController::class)->scoped();

// Customizing some route keys (only the task gets replaced)
Route::resource('users.tasks', TasksController::class)->scoped([
    'task' => 'slug',
]);

If we type hint our models in the controller methods using implicit route model binding, it will behave the same as the closure-based route above.

TasksController extends Controller
{
    // ...

    public function show(User $user, Task $task)
    {
        //
    }

   // ...
}

I don't think there is any breaking change so I chose the 7.x branch.

Also, if the PR is accepted, I'll open a PR for the docs as well.

@iamgergo
Copy link
Contributor Author

iamgergo commented Aug 4, 2020

I'm not sure why did the tests fail.

@driesvints If you'll have some time, could you restart the tests, please? Thanks!

@driesvints
Copy link
Member

@iamgergo seems you got it.

Did you test this with route caching btw?

@iamgergo
Copy link
Contributor Author

iamgergo commented Aug 4, 2020

@driesvints Well, I ran the php artisan route:cache command and it was working well. Or you meant something else?

Also, in the latest commit, I fixed the null column value by getting the default one using the getRouteKeyName method.

@driesvints
Copy link
Member

@iamgergo yep that's what I meant.

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.

3 participants