mercredi 29 novembre 2017

Laravel: How to use `Can` middleware

I want to give users that are logged in and are of a specific type (leader) access to all pages handled by a controller.

In AuthServiceProvider I created the following Gate

Gate::define('isLeader', function ($user) {
  return $user->isLeader();
});

Inside the controller that is only for logged in leaders, I have the following constructor

public function __construct()
{
    $this->middleware('auth');
    $this->middleware('can:isLeader');
}

For users who are a leader this works. But for users who are not a leader I get the error message This action is unauthorized. when accessing a page through this controller:

enter image description here

Instead of throwing an error message I would rather redirect the user to home if he is not a leader. How can I achive this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire