php - 如何在 Laravel 6 上定义 Gate
问题描述
我想定义一个门,但我不知道为什么不工作,知道吗?
我尝试 dd() on define,但没有显示 dd() 消息,重定向到页面「403 This action is authorized.」</p>
Laravel 6,
php 7.3
已经看到文档https://laravel.com/docs/6.x/authorization#gates
这是我的 AuthServiceProvider 和控制器
身份验证服务提供者
<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
'App\Models' => 'App\Policies\ModelPolicy',
];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
Gate::define('isAdmin', function ($user) {
dd('define isAdmin!'); // not show this message, redirect to 403 This action is unauthorized.
return $user->is_admin;
});
}
}
Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Gate;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
Gate::authorize('isAdmin');
}
}
解决方案
推荐阅读
- sympy - sympy 矩阵元素(分数--> 浮点数)?如何
- javascript - 具有回调函数的范围
- python - 最近邻加入距离条件
- xampp - Xampp (Ver 3.2) Mysql 无法启动
- python - 我的文件是保持打开还是自动关闭?
- python - 在 PyQt5 中,如何将普通的 QLineEdit(文本框)转换为完美的大写/大写 QLineEdit 框?
- css - CSS重复线性渐变线宽差异问题
- c - 如何使用 C 在 Postgres 中写入和读取字节
- amazon-web-services - IAM 角色密钥轮换时避免预签名 URL 过期
- c# - 属性名称作为 C# 中的 Lambda 表达式