1

We will need to add status code in to the response.

Like

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);
}

For now, we had modified library code on "ellipsesynergie/api-response/src/Laravel/Response.php".

But I feel. Its not good way.

Could you please suggest me how can i add this to all response without change library code ?

Or Is there any way to add the status code from controller for final response.

Actually, we will need final response like "http://screencast.com/t/nmWF7PYU".

Please help us on this.

Thanks in advance

Maulik Kanani
  • 642
  • 6
  • 22

1 Answers1

0

Just make your own Response class wich extends EllipseSynergie\ApiResponse\Laravel\Response. Let's call it App\ApiResponse\MyResponse

Overwrite all methods in EllipseSynergie\ApiResponse\Laravel\Response with your own. For example:

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

    return parent::withArray($array, $headers);
}

Then in your app\Providers\AppServiceProvider.php register you class something like:

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