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

[10.x] Add can middleware helper to resource routes and route registrar, improve consistency #42686

Closed
wants to merge 4 commits into from

Conversation

tontonsb
Copy link
Contributor

@tontonsb tontonsb commented Jun 7, 2022

I really enjoy the can method added in #39464. So much that I often run into situations where I try to use it but it's not available there (yet).

This PR adds the ability to use of can in these two constructions:

Route::resource('users', UserController::class)->can('manage-users');

Route::can('view-enums')->group(function () {
    Route::get('statuses', fn() => 'Ok');
});

To prevent can('action')->middleware('mw') or middleware('mw')->can('action') from overriding each other I changed the behaviour of ->middleware() to merge the middleware stack instead of overriding it:

Route::middleware('a')
    ->middleware('b')
    ->get('users', fn() => 'Ok');

Route::resource('users', UserController::class)
    ->middleware('a')
    ->middleware('b');

These previously lead to middleware stack ['b'], but now lead to middleware stack ['a', 'b'] and that is why this PR is a breaking change. This new behaviour is consistent with how ->middleware('x') behaves on a route (e.g. when chaining after Route::get()).

And one more consistency improvement is that comma-separated arg list can now be used to set multiple middlewares on resources, just like it was already available on Route::middleware() and Route::get()->middleware():

// Previously 'b' and 'c' would be discarded
Route::resource('users', UserController::class)->middleware('a', 'b', 'c');

@GrahamCampbell GrahamCampbell changed the title Add can middleware helper to resource routes and route registrar, improve consistency [10.x] Add can middleware helper to resource routes and route registrar, improve consistency Jun 7, 2022
@taylorotwell
Copy link
Member

I'm really hesitant to change that middleware merging behavior. I could see that really biting us 😬

@tontonsb
Copy link
Contributor Author

tontonsb commented Jun 8, 2022

@taylorotwell If I may continue the discussion a tiny bit, is this API inconsistency really intentional/desired?

// Merges middlewares to produce ['a', 'b']
Route::get('ok', fn() => 'ok')->middleware('a')->middleware('b');

// Overrides middlewares producing ['b']
Route::resource('users')->middleware('a')->middleware('b');

Or is the objection only about the merging on registrar? Or just my merge implementation looking fishy? 😂

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