Gate::before не вызывается, хотя в моем шаблоне лезвия есть аннотация @can.
Я написал обратный вызов before внутри AuthServiceProvider. Я проверил gate::after, он работает нормально.
namespace App\Providers;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
];
/**
* Register any authentication / authorization services.
*/
public function boot()
{
$this->registerPolicies();
Gate::before(function ($user, $ability) {
die('before called');
});
}
}
Почему нельзя называть Gate::before?
Вы зарегистрировали какие-либо ворота или полисы?
Пользователь вошел в систему?






Проверьте свою политику авторизации для промежуточного программного обеспечения. Конкретно:
'middleware' => ['web', 'admin.verify']
Подробнее о средстве авторизации Laravel: https://laravel.com/docs/5.7/authorization#via-middleware
По ссылке:
Laravel includes a middleware that can authorize actions before the incoming request even reaches your routes or controllers. By default, the
Illuminate\Auth\Middleware\Authorizemiddleware is assigned thecankey in yourApp\Http\Kernelclass.
use App\Post;
Route::put('/post/{post}', function (Post $post) {
// The current user may update the post...
})->middleware('can:update,post');
Попробуйте переключить эти настройки.
В документации Laravel говорится, что
The before method of a policy class will not be called if the class doesn't contain a method with a name matching the name of the ability being checked.