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

[11.x] middleware support for specific method in resource routes #53313

Draft
wants to merge 6 commits into
base: 11.x
Choose a base branch
from

Conversation

MrPunyapal
Copy link
Contributor

@MrPunyapal MrPunyapal commented Oct 26, 2024

Enhancement: Middleware Assignment for Resource & Singleton Routes

This pull request enables assigning middleware to specific resourceful methods on resource and singleton routes within the Laravel framework. Key changes include new method 'middlewareFor` for method-specific middleware assignment and updates to resource registration logic to support these configurations.

Motivation: While most developers prefer using routes for defining middleware, this particular case necessitates defining it at the controller level. Alternatively, we could extract all routes into separate routes. Addressing this challenge will streamline our approach.

Examples after merging:

Route::resource('users', UserController::class)->middlewareFor('show', 'auth');
Route::apiResource('users', UserController::class)->middlewareFor(['show', 'update'], 'auth');
Route::resource('users', UserController::class)
    ->middlewareFor('show', 'auth')
    ->middlewareFor('update', 'auth');
Route::apiResource('users', UserController::class)
    ->middlewareFor(['show', 'update'], ['auth', 'verified']);

Route::singleton('users', UserController::class)->middlewareFor('show', 'auth');
Route::apiSingleton('users', UserController::class)->middlewareFor(['show', 'update'], 'auth');
Route::singleton('users', UserController::class)
    ->middlewareFor('show', 'auth')
    ->middlewareFor('update', 'auth');
Route::apiSingleton('users', UserController::class)
    ->middlewareFor(['show', 'update'], ['auth', 'verified']);

I'm up for withoutMiddlewareFor if it's in demand.

… set middleware for specific methods on registered resource
…ability to set middleware for specific methods on registered resource
…t for setting middleware for specific methods on registered resources
@MrPunyapal MrPunyapal changed the title [WIP] [11.x] middleware support for specific method in resource routes [11.x] middleware support for specific method in resource routes Oct 26, 2024
@aniket-magadum
Copy link

Looks good to have 👍

@taylorotwell
Copy link
Member

One thing that gives me pause here is the order in which you call middleware and middlewareFor is not respected. The middlewareFor stuff is always put before middleware. What if you don't want that?

@taylorotwell taylorotwell marked this pull request as draft November 8, 2024 04:10
@MrPunyapal
Copy link
Contributor Author

One thing that gives me pause here is the order in which you call middleware and middlewareFor is not respected. The middlewareFor stuff is always put before middleware. What if you don't want that?

Thanks for reviewing and taking an interest in my PR! I'll take a look soon.

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