Pass arguments to middleware

You can pass arguments to your middleware for specific routes by appending ':' followed by the value. For example, I'm enforcing different authentication methods based on the route using a single middleware.

1Route::get('...')->middleware('auth.license');
2Route::get('...')->middleware('auth.license:bearer');
3Route::get('...')->middleware('auth.license:basic');
1class VerifyLicense
2{
3 public function handle(Request $request, Closure $next, $type = null)
4 {
5 $licenseKey = match ($type) {
6 'basic' => $request->getPassword(),
7 'bearer' => $request->bearerToken(),
8 default => $request->get('key')
9 };
10 
11 // Verify license and return response based on the authentication type
12 }
13}

Tip given by @Philo01

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord