请求基本流程

基本请求流程

路由 -> 中间件 -> 表单验证 -> 资源控制器 -> 服务层(也就是逻辑层) -> 仓库 -> 模型 -> 响应资源

  • 仓库 跟 模型的关系 ?
    在仓库层定义 curd
    在模型中定义表 关联关系

异常处理

  • 因为是基于 laravels 进行开发, 所以在代码全部采用抛出异常处理, 参考文章:

  • 响应资源状态码使用 HTTP Status Code, 如:

    • 成功 200
    • 资源不存在 404
  • HTTP Status Code 的使用
    使用在 Symfony\Component\HttpFoundation\Response 已经声明好的状态

  • 异常级别定义

    • Error
    • Warning
    • Debug

异常类的定义

参考:

自定义接口错误响应格式

BaseExceptions异常类

  • 使用定义好的 convertMessageFormat 进行错误提示信息转换为系统语言包提示信息, 进行统一处理
  • reportrender 在异常类定义的原因是尽可能的不干涉系统原有逻辑
  • FormatOutput 在该 特征 中定义输出格式
/**
 * 基础异常处理
 *
 * @package App\Exceptions
 */
abstract class BaseExceptions extends Exception
{
    use FormatOutput;

    /**
     * 错误状态码
     *
     * @var int
     */
    protected $code = Response::HTTP_OK;

    /**
     * 错误信息
     *
     * @var string
     */
    protected $message = '';

    /**
     * 错误信息操作
     *
     * @var array
     */
    protected $messageOption = [];

    /**
     * BaseExceptions constructor.
     *
     * @param int            $code
     * @param string         $message
     * @param array       $messageOption
     * @param Throwable|null $previous
     *
     * @throws BindingResolutionException
     */
    public function __construct(
        int $code = 0,
        string $message = "",
        array $messageOption = [],
        Throwable $previous = null
    ) {

        $this->setCode($code)
            ->setMessage($message)
            ->setMessageOption($messageOption);

        parent::__construct($message, $code, $previous);
    }

    /**
     * 设置错误状态码
     *
     * @param int $code
     *
     * @return self
     */
    public function setCode(int $code): self
    {
        $this->code = $code;

        return $this;
    }

    /**
     * 设置信息操作
     *
     * @param array $messageOption
     *
     * @return $this
     */
    public function setMessageOption(array $messageOption)
    {
        $this->messageOption = $messageOption;

        return $this;
    }

    /**
     * 获取信息操作
     *
     * @return array
     */
    public function getMessageOption(): array
    {
        return $this->messageOption;
    }

    /**
     * 设置错误信息
     *
     * @param string $message
     *
     * @return $this
     * @throws BindingResolutionException
     */
    public function setMessage(string $message): self
    {
        $this->message = $this->convertMessageFormat($message);

        return $this;
    }

    /**
     * 转换消息格式化
     *
     * @param string $message
     *
     * @return string
     * @throws BindingResolutionException
     */
    protected function convertMessageFormat(string $message)
    {
        $translator = Container::getInstance()->make('translator');
        if ($translator->has($message)) {
            return $message;
        }

        return $translator->get($message, $this->getMessageOption());
    }

    /**
     * 自定义通知记录
     *
     * @return void
     */
    abstract public function report();

    /**
     * 自定义抛出异常信息
     *
     * @return JsonResponse
     */
    abstract public function render();
}

FormatOutput 特征

/**
 * 定义异常输出格式
 *
 * @package App\Exceptions
 */
trait FormatOutput
{
    /**
     * 默认使用header
     *
     * @var array
     */
    private $defaultHeader = [
        //格式说明
        "Content-Type" => "application/json"
    ];

    /**
     * 用户自定义header
     *
     * @var array
     */
    protected $header = [];

    /**
     * 获取数据格式
     *
     * @return array
     */
    protected function getDataFormat(): array
    {
        return [
            "message" => $this->getMessage(),
            "data" => []
        ];
    }

    /**
     * 设置header
     *
     * @param array $header
     *
     * @return $this
     */
    public function setHeader(array $header): self
    {
        $this->header = $header;

        return $this;
    }

    /**
     * 获取 header
     *
     * @return array
     */
    protected function getHeader(): array
    {
        return array_merge($this->defaultHeader, $this->header);
    }

    /**
     * 获取响应内容
     *
     * @return JsonResponse
     */
    protected function getContent(): JsonResponse
    {
        return new JsonResponse(
            $this->getDataFormat(),
            $this->getCode(),
            $this->getHeader(),
            JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
        );
    }
}

ErrorExceptions 继承演示

  • 目前 report 暂时还没考虑其它处理, 所以只是定义日志记录
/**
 * 致命错误
 *
 * @package App\Exceptions
 */
class ErrorExceptions extends BaseExceptions
{

    /**
     * @inheritDoc
     */
    public function report()
    {
        Log::error($this->getMessage());
    }

    /**
     * @inheritDoc
     */
    public function render()
    {
        return $this->getContent();
    }
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

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