准备

如果框架是由5.0升级而来的,那么像要使用权限管理功能是需要稍作修改的。一下内容来自官方升级指南

Create The Policies Directory

First, create an empty app/Policies directory within your application.

Create / Register The AuthServiceProvider & Gate Facade

Create a AuthServiceProvider within your app/Providers directory. You may copy the contents of the default provider from GitHub. Remember to change the provider's namespace if your application is using a custom namespace. After creating the provider, be sure to register it in your app.php configuration file's providers array.

Also, you should register the Gate facade in your app.php configuration file's aliases array:

'Gate' => IlluminateSupportFacadesGate::class,

Update The User Model

Secondly, use the IlluminateFoundationAuthAccessAuthorizable trait and IlluminateContractsAuthAccessAuthorizable contract on your AppUser model:

<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword;
}

Update The Base Controller

Next, update your base AppHttpControllersController controller to use the IlluminateFoundationAuthAccessAuthorizesRequests trait:

<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

abstract class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}

Laravel

登陆发表评论