讨论数量:
app/Exceptions/Handler.php中统一处理就好
use Spatie\Permission\Exceptions\PermissionDoesNotExist;
// 权限失败
$this->renderable(function (PermissionDoesNotExist $e) {
//todo 日志,上报等
return $this->response($e->getCode(), $e->getMessage());
});
我是依赖于 laravel-permission
基础上封装了一层权限处理方式
public function destroy(User $user)
{
\throw_can(UserPermission::delete());
}
UserPermission
文件
<?php
namespace App\Permissions;
/**
* @method static bool root()
* @method static bool create()
* @method static bool edit()
* @method static bool delete()
* @method static bool possessed()
* @method static bool resetPassword()
*/
class UserPermission extends BasePermission
{
//system.user => 用户列表
//system.user.create => 用户新增
//system.user.edit => 用户编辑
//system.user.delete => 用户删除
//system.user.possessed => 用户附身
//system.user.reset-password => 重置密码
public static function getParentPermission()
{
return 'system'; // TODO: Change the autogenerated stub
}
}
permission 文件可以直接写个stub 模版去生成,异常的话全局也能对应拦截 ,前面是有配置文件的 以上可以参考
推荐文章: