Usage Examples of laravel Framework Middleware except and only

  • 2021-12-13 07:40:40
  • OfStack

This article illustrates the usage of laravel framework middleware except and only. Share it for your reference, as follows:

except

except: It is a blacklist mechanism. Except for show page, which is not filtered by middleware Auth, all other pages need to be filtered. If it fails to pass the verification, it will jump to the specified page

only

only: It is a whitelist mechanism. Except edit pages need to be filtered by middleware Auth, other pages do not need to be filtered. If it fails to pass the verification, it will jump to the specified page

except Usage:


class UserController extends Controller
{
  public function __construct()
  {
    $this->middleware('auth', ['except' => 'show']);
   }
  public function show(User $user)
  {
    return view('users.show', compact('user'));
  }
 public function edit(User $user)
  {
     return view('users.edit', compact('user'));
  }
}

except: It is a blacklist mechanism. Except for show page, which is not filtered by middleware Auth, all other pages need to be filtered. If it fails to pass the verification, you will jump to the specified page

only Usage:


class UserController extends Controller
{
  public function __construct()
  {
    $this->middleware('auth', ['only' => 'edit']);
   }
  public function show(User $user)
  {
    return view('users.show', compact('user'));
  }
 public function edit(User $user)
  {
     return view('users.edit', compact('user'));
  }
}

only: It is a whitelist mechanism. Except edit pages need to be filtered by middleware Auth, other pages do not need to be filtered. If it fails to pass the verification, it will jump to the specified page

More readers interested in Laravel can check the topics of this site: "Introduction and Advanced Tutorial of Laravel Framework", "Summary of Excellent Development Framework of php", "Introduction Tutorial of php Object-Oriented Programming", "Introduction Tutorial of php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"

Hope that this article is based on the framework of Laravel PHP programming help.


Related articles: