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 a middleware from route registration #32347

Merged
merged 3 commits into from
Apr 13, 2020
Merged

[7.x] Ability to skip a middleware from route registration #32347

merged 3 commits into from
Apr 13, 2020

Conversation

dsazup
Copy link
Contributor

@dsazup dsazup commented Apr 12, 2020

Hello. Quite a few times I needed to skip a specific middleware for just one route. The current approach seems to be $except property in the middleware and then skipping it if the URL matches. This mostly works, but sometimes we do not know the exact URL, or we do not know the name of the route so that is not always the best solution.

I would like to propose skipMiddleware method, which would be used like:

  • Route::get('/something')->skipMiddleware(VerifyCsrfToken::class)
  • Route::get('/teams/create')->skipMiddleware(VerifyUserHasTeam::class)

Quite simple, but solves an annoying problem. Hopefully, I am not the only one running into this so I'd love to see this in the core. Please consider it and let me know if there is anything I need to change.

Thanks!

@taylorotwell
Copy link
Member

taylorotwell commented Apr 13, 2020

Did you try this with the new route caching system? Does it still work?

@dsazup
Copy link
Contributor Author

dsazup commented Apr 13, 2020

I just tried it, and it seems to work. Not the most useful example, but I tested with this in web.php

Route::middleware('auth')->group(function () {
    Route::get('/', [HomeController::class, 'index'])->skipMiddleware(Authenticate::class);
});

after running artisan route:cache command routes-v7.php contains:
Screenshot 2020-04-13 at 16 42 17

and sure enough if I visit that route without being authenticated it works (i.e middleware is skipped successfully). I'm not sure if I should add a separate test for it?

@taylorotwell
Copy link
Member

Looks like it is probably covered under the action being serialized.

@taylorotwell taylorotwell merged commit 346b131 into laravel:7.x Apr 13, 2020
@taylorotwell
Copy link
Member

Fixed a small issue to resolve short alias names to class names before the exclusion check. Thanks.

@dsazup
Copy link
Contributor Author

dsazup commented Apr 13, 2020

Awesome, thanks!

@driesvints
Copy link
Member

@dsazup was it intentional that this doesn't applies to global middleware?

@dsazup
Copy link
Contributor Author

dsazup commented Apr 16, 2020

Hey @driesvints, not really. I didn't realize the global middleware is being applied elsewhere, but now I see it happens in the http kernel. It would be really good to get it to work with global middleware though. 🤔

@driesvints
Copy link
Member

@dsazup cool. Could you send in a PR for that? Here's a related issue: #32384

@decadence
Copy link
Contributor

Is this renamed to withoutMiddleware?

@driesvints
Copy link
Member

@decadence yes.

@Claw-Hammer
Copy link

Claw-Hammer commented Apr 17, 2020

I just tried this and didn't worked for me, i did tried with ->skipMiddleware(VerifyCsrfToken::class), and with ->withoutMiddleware(VerifyCsrfToken::class).... and nothing

@BrandonSurowiec
Copy link
Contributor

BrandonSurowiec commented Apr 17, 2020

@Claw-Hammer This doesn't support excluding global middleware: #32404

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.

6 participants