Ever needed to apply middleware to specific controller methods? Just use the middleware()
method in the controller's constructor and feed it a middleware and array of method names.
Protip: You can stack middleware as much as you want (just don't lock yourself out!). If you need to apply different middleware, or complex 'only' and 'except' situations, call the middleware method as much as you need.
<?php
//... the class
public function __construct()
{
// Apply middleware to only certain routes
$this->middleware('auth', ['only' => ['create', 'store', 'edit', 'delete']]);
// Or apply middleware to all routes except these
$this->middleware('auth', ['except' => ['index', 'show']]);
}
Before writing this post, this wasn't in the Laravel docs. I could have swore I've seen it since, but just in case, I've archived it here.
Hope that helps, Ryo
References