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

[8.x] Adds dynamic middleware set on route declaration #39483

Closed
wants to merge 8 commits into from
Closed

[8.x] Adds dynamic middleware set on route declaration #39483

wants to merge 8 commits into from

Conversation

DarkGhostHunter
Copy link
Contributor

@DarkGhostHunter DarkGhostHunter commented Nov 4, 2021

What?

Based on #39464. Allows dynamically setting middleware in route declarations, including parameters, fluently.

// Before
Route::get('flights', [FlightController::class , 'show'])
    ->middleware(['can:viewAny:\App\Models\Flight', 'auth:pilot']);

// After
Route::get('flights', [FlightController::class , 'show'])
    ->can('viewAny', Flight::class)
    ->auth('pilot');

How?

When passing a method that doesn't exist or is not defined as a macro when declaring a route, the router will check if the method name is set as route middleware. If that's the case, it will set is as a middleware for the given route.

If these conditions don't met, an BadMethodCallException will be thrown. This avoids the developer to push any typo accidentally, where middleware() would, pushing the error when the route is called. For this to work, an app(Kernel::class) was implemented to check the registered array.

It also works on dot notation middleware, like password.confirm, with parameters:

// Before
Route::get('flights', [FlightController::class , 'show'])
    ->middleware('password.confirm:auth.confirm');

// After
Route::get('flights', [FlightController::class , 'show'])
    ->passwordConfirm('auth.confirm');

It will also work when:

  • using the Router facade directly:
Route::passwordConfirm()->get('flights', ...);
  • using a route group
Route::auth()->group(fn() => ... );
  • using a resource
Route::resource('flight')->authBasic();

BC?

  1. No, as is expected route declarations using the available methods.
  2. No, there are no method collisions when cross-checking the default route middleware.
  3. No. If there is a macro with the same name, macro takes precedence.

Notes

Because it uses app(Kernel::class)->getRouteMiddleware() behind the scenes to check if the middleware exists or a BadMethodCallException should be thrown, some tests must be rewritten to add a container instance.

@GrahamCampbell GrahamCampbell changed the title [8.x] Adds dynamic middleware set on route declaration. [8.x] Adds dynamic middleware set on route declaration Nov 4, 2021
@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions!

If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response.

@DarkGhostHunter DarkGhostHunter deleted the feat/dynamic-middleware branch November 12, 2021 17:04
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