Skip to content

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

Merged
taylorotwell merged 3 commits into
laravel:7.xfrom
dsazup:skip-middleware
Apr 13, 2020
Merged

[7.x] Ability to skip a middleware from route registration#32347
taylorotwell merged 3 commits into
laravel:7.xfrom
dsazup:skip-middleware

Conversation

@dsazup

@dsazup dsazup commented Apr 12, 2020

Copy link
Copy Markdown
Contributor

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

taylorotwell commented Apr 13, 2020

Copy link
Copy Markdown
Member

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

@dsazup

dsazup commented Apr 13, 2020

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
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
Copy Markdown
Member

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

@dsazup

dsazup commented Apr 13, 2020

Copy link
Copy Markdown
Contributor Author

Awesome, thanks!

@driesvints

Copy link
Copy Markdown
Member

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

@dsazup

dsazup commented Apr 16, 2020

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
Member

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

@decadence

Copy link
Copy Markdown
Contributor

Is this renamed to withoutMiddleware?

@driesvints

Copy link
Copy Markdown
Member

@decadence yes.

@Claw-Hammer

Claw-Hammer commented Apr 17, 2020

Copy link
Copy Markdown

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

BrandonSurowiec commented Apr 17, 2020

Copy link
Copy Markdown
Contributor

@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