前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Laravel 如何优雅的实现输出结构统一的功能?

Laravel 如何优雅的实现输出结构统一的功能?

作者头像
写PHP的老王
发布2019-08-12 15:39:04
2K0
发布2019-08-12 15:39:04
举报
文章被收录于专栏:写PHP的老王写PHP的老王

背景

一般的项目需求都会要求统一的输出结构,特别是对于api应用而言。因此,如果有beforeResponse的功能,则可以在数据输出之前对response进行统一格式化处理。

假设这么一种场景,应用做api开发,使用抛异常的方式(自定义异常类ApiException)返回无效非法请求的情况。正常请求则返回合法数据(数组或可序列化的模型),希望返回的数据格式

正常请求返回数据格式:

代码语言:javascript
复制
{
  "code":0,
  "data":[

  ],
  "message":""
}

异常请求返回数据格式:

代码语言:javascript
复制
{
  "code":400,
  "data":[

  ],
  "message":"错误提示"
}

Laravel 的设计如何实现

Laravel中的中间件确实支持beforeResponse操作,支持在中间件中进行格式化。但是,这里仅限于正常返回。那么如果控制器抛了异常又改怎么办呢?

Laravel的调用链使得控制器里的异常在正常情况下,还没有抛到中间件就被系统注册的ExceptionHandler类拦截处理了。github上也有关于中间件不能捕获控制器异常的问题Can't catch exception in middleware

作者给出的结论是,Laravel本身的设计就是将异常处理放在ExceptionHandler中。

Yes, this is the beavhiour starting from L5.2. Throwing an exception causes the response to be set as that returned from the exception handler, and then the middleware is allowed to backout from that point.

We don't recommend you write try catch blocks in middleware. Instead, handle exceptions inside the exception handler. Perhaps https://github.com/GrahamCampbell/Laravel-Exceptions would be of use to you?

那么,按照Laravel的设计,正常的请求,我们在一个中间件FormaterResponse处理,处理逻辑如下:

代码语言:javascript
复制

<?php
namespace App\Http\Middleware;
use App\Http\Middleware\Closure;
use \Exception;
class FormaterResponse
{
  public function handle($request, \Closure $next)
  {
    $response = $next($request);
    $content = $response->getData();
    $content = [
      'code'=>0,
      'message'=>'',
      'data'=>$content
    ];
    $response->setData($content);
    return $response;
  }
}


错误返回,我们在app\Exceptions\Handlerrender方法处理,格式化,处理逻辑如下:

代码语言:javascript
复制

public function render($request, Exception $e)
{
  if($e instanceof ApiException)
  {
    $response = [
      'code'=>$e->getCode(),
      'message'=>$e->getMessage(),
      'data'=>[]
    ];
    return response()->json($response, 200);
  }
  parent::render($request,$e);
}

更好的方式

上面的这种做法有一个弊端,如果某些模块下想要的数据格式返回不一样,对应异常情况的处理会比较麻烦。因为ExceptionHandler是对一个全局的处理。如果能把数据格式化都放在中间件处理,则可以非常灵活。

其实需要改动的内容非常上,只需要在ExceptionHandler中的handle方法中,对于自定义异常类ApiException继续向上抛出去就可以在middleware捕获到异常,进而对异常放回进行格式化。

修改之后App\Exceptions\Handler 中render的代码如下:

代码语言:javascript
复制
public function render($request, Exception $e)
{
  if($e instanceof ApiException)
  {
    throw $e;
  }
  parent::render($request,$e);
}
代码语言:javascript
复制
<?php

namespace App\Http\Middleware;
use App\Http\Middleware\Closure;
use App\Exceptions\ApiException;
class FormaterResponse
{
  public function handle($request, \Closure $next)
  {
    $code = 0;
    $msg = '';
    $data = [];
    try{
      $response = $next($request);
      $data = $response->getData();
    }catch(ApiException $e){
      $code = $e->getCode();
      $msg = $e->getMessage();
      $response = response()->json([],200);
    }
    $content = [
      'code'=>$code,
      'message'=>$msg,
      'data'=>$data
    ];
    $response->setData($content);
    return $response;
  }
}

这样就可以在所有应用FormaterResponse的路由中实现beforeRespons 功能,格式化统一的数据输出。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-07-31,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 写PHP的老王 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 背景
  • Laravel 的设计如何实现
  • 更好的方式
相关产品与服务
消息队列 TDMQ
消息队列 TDMQ (Tencent Distributed Message Queue)是腾讯基于 Apache Pulsar 自研的一个云原生消息中间件系列,其中包含兼容Pulsar、RabbitMQ、RocketMQ 等协议的消息队列子产品,得益于其底层计算与存储分离的架构,TDMQ 具备良好的弹性伸缩以及故障恢复能力。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档