Last active
August 9, 2016 09:37
-
-
Save IftekherSunny/fbb32d56352c4f7c30485461a31cad1c to your computer and use it in GitHub Desktop.
Http response trait for the Laravel framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
trait HttpResponse | |
{ | |
/** | |
* Response unprocessable entity. | |
* | |
* @param array $message | |
* | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
protected function responseUnprocessableEntity(array $message = []) | |
{ | |
return response()->json(array_merge($message, [ | |
'status_code' => 422 | |
]), 422); | |
} | |
/** | |
* Response already exist. | |
* | |
* @param array $message | |
* | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
protected function responseAlreadyExist(array $message = []) | |
{ | |
return response()->json(array_merge($message, [ | |
'status_code' => 423 | |
]), 422); | |
} | |
/** | |
* Response new item created. | |
* | |
* @param array $message | |
* | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
protected function responseCreated(array $message = []) | |
{ | |
return $this->responseOk(array_merge($message, ['created' => true])); | |
} | |
/** | |
* Response item not found. | |
* | |
* @param array $message | |
* | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
protected function responseNotFound(array $message = []) | |
{ | |
return response()->json(array_merge($message, [ | |
'status_code' => 404 | |
]), 404); | |
} | |
/** | |
* Response ok. | |
* | |
* @param array $message | |
* | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
protected function responseOk(array $message = []) | |
{ | |
return response()->json(array_merge($message, [ | |
'status_code' => 200 | |
]), 200); | |
} | |
/** | |
* Response server error. | |
* | |
* @param array $message | |
* | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
protected function responseServerError(array $message = []) | |
{ | |
return response()->json(array_merge($message, [ | |
'status_code' => 500 | |
]), 500); | |
} | |
/** | |
* Response json. | |
* | |
* @param $message | |
* | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
protected function responseJson($message) | |
{ | |
if(! is_array($message)) { | |
return $this->responseOk($message->toArray()); | |
} | |
return $this->responseOk($message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment