spatie/Laravel-permission Laravel 的异常抛出信心优化,有大佬做过吗

1.使用了 spatie/Laravel-permission Laravel 做权限,现在想优化一下没权限抛出信息,大家知道怎么弄吗

本作品采用《CC 协议》,转载必须注明作者和本文链接
方丈
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
讨论数量: 4

app/Exceptions/Handler.php中统一处理就好

use Spatie\Permission\Exceptions\PermissionDoesNotExist;
// 权限失败
$this->renderable(function (PermissionDoesNotExist $e) {
    //todo 日志,上报等
    return $this->response($e->getCode(), $e->getMessage());
});
1年前 评论

完全可以加一个新的中间件来做鉴权和抛出异常。没必要使用全局错误处理、

1年前 评论

我是依赖于 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 模版去生成,异常的话全局也能对应拦截 ,前面是有配置文件的 以上可以参考

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!