定制 liuchang103/hyperf-exception 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

liuchang103/hyperf-exception

最新稳定版本:1.1.1

Composer 安装命令:

composer require liuchang103/hyperf-exception

包简介

hyperf exception api handler

关键字:

README 文档

README

Hyperf 的异常错误处理,有 Json 和 View 自定义渲染

安装

引入包

composer require liuchang103/hyperf-exception

使用

直接抛出 Json

throw new \HyperfException\Json('Error', -1);

// 输出
{"code":-1,"message":"Error"}

直接抛出 View 视图

throw new \HyperfException\View('Error', '404');

// 将抛出 404 的视图文件

进阶

继承 Json

使用常量约定 code 码,让控制器中消失 “数字”

namespace App\Exception;

class Success extends \HyperfException\Json
{
    const CODE = 100;
}

// 抛出
throw new \App\Exception\Success('Created Success');

// 输出
{"code":100,"message":"Created Success"}

定义 Http 状态码

class Error extends \HyperfException\Json
{
    const CODE = 200;
    const HTTP_STATUS = 500;
}

// 抛出
throw new \App\Exception\Error('Created Error');

// 将抛出 500 的 Http 状态
{"code":200,"message":"Created Error"}

定义 json 格式

class Error extends \HyperfException\Json
{
    const CODE = -1;
    const HTTP_STATUS = 500;

    // 自定义 json 格式
    protected function json()
    {
        return [
            'errcode' => $this->getCode(),
            'errmessage' => $this->getMessage()
        ];
    }
}

// 抛出
throw new \App\Exception\Error('Created Error');

// 将抛出 500 的 json 格式
{"errcode":-1,"errmessage":"Created Error"}

自定义传参

class Error extends \HyperfException\Json
{
    const CODE = -1;
    const HTTP_STATUS = 500;

    public function __construct($message, $model)
    {
        parent::__construct($message);

        $this->model = $model;
    }

    // 自定义 json 格式
    protected function json()
    {
        return [
            'errcode' => $this->getCode(),
            'errmessage' => $this->getMessage(),
            'model' => $this->model
        ];
    }
}

// 抛出
throw new \App\Exception\Error('Update Error', $model);

// 将抛出 500 的 json 格式
{"errcode":-1,"errmessage":"Update Error", "model":{}}

继承 View

使用常量约定 VIEW,让控制器中消失 “模版名”

namespace App\Exception;

class Miss extends \HyperfException\View
{
    const VIEW = '404';
}

// 抛出
throw new \App\Exception\Miss('找不到存在的页面');

// 视图文件 404.blade.php
<h1> {{ $message }} </h1>

定义 Http 状态码

class Miss extends \HyperfException\View
{
    const VIEW = '404';
    const HTTP_STATUS = 404;
}

// 抛出
throw new \App\Exception\Miss('找不到存在的页面');

自定义视图传参

class Miss extends \HyperfException\View
{
    const VIEW = '404';
    const HTTP_STATUS = 404;

    protected $model;

    public function __construct($message, $model)
    {
        parent::__construct($message);

        $this->model = $model;
    }

    // 自定义视图参数
    protected function view()
    {
        return [
            'model' => $this->model
        ];
    }
}

// 抛出
throw new \App\Exception\Miss('Error', $model);

// 404.blade.php
{{ $model->name }} 访问页面错误

自定义处理

如果需要自定义渲染,可继承 \HyperfException\Exception 并实现 render 方法,会接收 response 的对象

use HyperfException\Exception;
use Hyperf\HttpMessage\Stream\SwooleStream;

class Message extends Exception
{
    const HTTP_STATUS = 202;
    
    // 自定义渲染
    public function render($response)
    {
        return $response->withBody(new SwooleStream($this->getMessage() . $this->getCode()));
    }
}

// 抛出
throw new \App\Exception\Message('自定义渲染', 1);

// 输出 202 的状态页面
自定义渲染1

HTTP_STATUS 状态码是在更深一层的自定义处理中.

如果要自定义处理,可实现 handle 方法,同样会接收 response 的对象

(注意:实现 handle 方法后,HTTP_STATUS 常量将无效,render 渲染方法也将无效)

public function handle($response)
{
    return $response->withBody(new SwooleStream($this->getMessage() . $this->getCode()));
}

// 输出 200 正常状态的页面
自定义渲染1

统计信息

  • 总下载量: 3.46k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-07-30

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固