PHP Classes

File: apps/ApiGateway/app/Traits/ApiResponser.php

Recommend this page to a friend!
  Classes of mohammad anzawi   PHP Hotel Booking Available   apps/ApiGateway/app/Traits/ApiResponser.php   Download  
File: apps/ApiGateway/app/Traits/ApiResponser.php
Role: Class source
Content type: text/plain
Description: Class source
Class: PHP Hotel Booking Available
Search for hotels that are available for booking
Author: By
Last change:
Date: 2 years ago
Size: 1,260 bytes
 

Contents

Class file image Download
<?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');
    }
}