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] Ability to skip middleware from resource routes #32891

Merged
merged 1 commit into from
May 19, 2020

Conversation

sebdesign
Copy link
Contributor

This PR adds the ability to skip specific middleware from resources routes. This is similar to #32347. This is handy when having middleware groups with many resourceful routes but you need to exclude some middleware from a specific resource.

Example:

<?php
Route::middleware(['auth', 'verified'])->group(function () {
    Route::resource('photo', 'PhotosController')->withoutMiddleware('verified');
});

@taylorotwell taylorotwell merged commit ec38179 into laravel:7.x May 19, 2020
@GrahamCampbell GrahamCampbell changed the title Ability to skip middleware from resource routes [7.x] Ability to skip middleware from resource routes May 19, 2020
@jasonmccreary
Copy link
Contributor

Curious how hard this would be to add to any route. 🤔

I could see similar use cases where I want a majority of actions to have a certain middleware, but only one without. While this can be done within the controller, it'd be nice to have the ability to define it all in a single route definition.

@sebdesign
Copy link
Contributor Author

@jasonmccreary could be done with the same syntax as the names() method.

If the $middleware argument is an associative array, the keys should match the resource actions and the values should match the unwanted middleware.

<?php

Route::resource('photo', 'PhotosController')->withoutMiddleware([
    'index' => ['auth', 'verified'],
    'create' => 'verified',
]);

If the maintainers are ok with that, the middleware() method should behave the same way maybe:

<?php

Route::resource('photo', 'PhotosController')->middleware([
    'create' => 'auth',
    'store' => 'auth',
    'destroy' => ['auth', 'admin'],
]);

@dsazup
Copy link
Contributor

dsazup commented May 23, 2020

Something like this would be awesome. Needed this just now for a single route in a resource controller. 👍

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.

4 participants