Gate::before
没有被调用,即使我的刀片模板中有@can
注释。
我已经在AuthServiceProvider
中编写了before
回调。我检查了gate::after
,它运行良好。
namespace AppProviders;
use IlluminateSupportFacadesGate;
use IlluminateFoundationSupportProvidersAuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
'AppModel' => 'AppPoliciesModelPolicy',
];
/**
* 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-中间件
来自链接:
Laravel包含一个中间件,它可以在传入请求到达您的路由或控制器之前授权操作。默认情况下,
IlluminateAuthMiddlewareAuthorize
中间件在AppHttpKernel
类中被分配了can
密钥。
use AppPost;
Route::put('/post/{post}', function (Post $post) {
// The current user may update the post...
})->middleware('can:update,post');
尝试切换这些设置。