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

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

本作品采用《CC 协议》,转载必须注明作者和本文链接
方丈
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 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年前 评论

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