<?php
/**
* as a file name this trait to handle our API responses.
* we need all responses to be JSON response.
*/
namespace App\Traits;
use Illuminate\Http\Response;
trait ApiResponser
{
/**
* Success Response
* @param $data
* @param int $code
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|Response|\Laravel\Lumen\Http\ResponseFactory
*/
public function successResponse($data, $code = Response::HTTP_OK)
{
return response($data, $code)->header('Content-Type', 'application/json');
}
/**
* Error Response
* @param $message
* @param $code
* @return \Illuminate\Http\JsonResponse
*/
public function errorResponse($message, $code)
{
return response()->json(['error' => $message, 'code' => $code], $code);
}
/**
* Response a messages error
* @param $message
* @param $code
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|Response|\Laravel\Lumen\Http\ResponseFactory
*/
public function errorMessage($message, $code)
{
return response($message, $code)->header('Content-Type', 'application/json');
}
}
|