Laravel - Controllers - Controller Middleware

Middleware may be assigned to the controller's routes in your route files:

    
    Route::get('profile', [UserController::class, 'show'])->middleware('auth');
	

Or, you may find it convenient to specify middleware within your controller's constructor. Using the middleware method within your controller's constructor, you can assign middleware to the controller's actions:

    
    class UserController extends Controller
    {
        /**
         * Instantiate a new controller instance.
         *
         * @return void
         */
        public function __construct()
        {
            $this->middleware('auth');
            $this->middleware('log')->only('index');
            $this->middleware('subscribed')->except('store');
        }
    }
	

Controllers also allow you to register middleware using a closure. This provides a convenient way to define an inline middleware for a single controller without defining an entire middleware class:

    
    $this->middleware(function ($request, $next) {
        return $next($request);
    });