首页 > 解决方案 > 如何在 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');
        }
    }

标签: phplaravellaravel-6

解决方案


推荐阅读