【问题标题】:How to add HTTP Status code and status text in to response with ellipsesynergie/api-response In Laravel 5?如何在 Laravel 5 中使用 ellipsesynergie/api-response 添加 HTTP 状态代码和状态文本以响应?
【发布时间】:2015-07-24 15:46:18
【问题描述】:

我们需要在响应中添加状态码。

喜欢

public function withArray(array $array, array $headers = array()) {
if ($this->statusCode == '200') {
$array = array('code' => $this->statusCode , 'success' => TRUE) + $array;
} else {
$array = array('code' => $this->statusCode, 'success' => FALSE) + $array;
}
return response()->json($array, $this->statusCode, $headers);
}

目前,我们已经修改了 "ellipsesynergie/api-response/src/Laravel/Response.php" 上的库代码。

但我觉得。这不是一个好方法。

您能否建议我如何在不更改库代码的情况下将其添加到所有响应中?

或者有什么方法可以从控制器添加状态码以获得最终响应。

实际上,我们需要像“http://screencast.com/t/nmWF7PYU”这样的最终回复。

请在这方面帮助我们。

提前致谢

【问题讨论】:

    标签: php laravel-5 http-status-code-301 laravel-response


    【解决方案1】:

    只需创建自己的 Response 类,扩展 EllipseSynergie\ApiResponse\Laravel\Response。我们就叫它App\ApiResponse\MyResponse

    用您自己的方法覆盖EllipseSynergie\ApiResponse\Laravel\Response 中的所有方法。 例如:

    public function withArray(array $array, array $headers = array())
    {
        $array['code'] = $this->statusCode;
        $array['success'] = true;
    
        return parent::withArray($array, $headers);
    }
    

    然后在你的app\Providers\AppServiceProvider.php 注册你的类,比如:

    $myResponse = new \App\ApiResponse\MyResponse(new \League\Fractal\Manager());    
    $this->app->instance('EllipseSynergie\ApiResponse\Contracts\Response', $myResponse);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2014-01-08
      相关资源
      最近更新 更多