cerenimo/laravel-responses

Standardized responses for Laravel.

v1.2.3 2024-05-02 08:27 UTC

README

composer require cerenimo/laravel-responses

The ResponseTrait is a trait used to standardize the HTTP response messages in Laravel and Lumen. You can install it using the composer command mentioned above. To use it in your class, add the following line with the "use" keyword:

use Cerenimo\LaravelResponses\Response;

class YourClass {

use Response;
// ...
}

Inside your function, you can use it as follows:

return $this->setMessage('successfully')
->responseSucces();

Response / 200 OK

{
    "result": true,
    "message": "successfully"
}


Parameters:
  • setCustomData($key,$value)
  • setMessage($message)
  • setStatusCode($statusCode)
  • setValidation($validation)
  • setException($exception)
  • setPagination($pagination)
  • setDataName($dataName)



The package provides the following functions:
responseSuccess()
  • Use if the request is successful.

    • Parameters optional. setCustomData, setMessage and setStatusCode can be added.

    • Usage examples:

          $this->setCustomData('user', $user)
          ->setCustomData('token', $token)
          ->setMessage('User Created')
          ->setStatusCode(201)
          ->responseSuccess(); 
          $this->responseSuccess();
          $this->setMessage('Successfully!')->responseSuccess();
    • Response example:

      {
          "result": true,
          "message": "User Created",
          "data": {
              "user": {
                  "id": 372,
                  "name": "Ceren Özkurt"
              },
              "token" : "Bfafa98a7f8afkafafaf98afajfka"
          }
      }

responseDataWithPagination()
  • Use if the server does not support the features required to fulfill the request.
    • setPagination should be added and setMessage and setDataName can be added.

    • Usage examples:

          $this->setPagination($setPagination)
          ->setDataName('files')
          ->responseDataWithPagination(); 
    • Response example:

      {
          "result": true,
          "data": {
              "files": [
                  {
                      "id": 60,
                      "userId": "auth0|64a7fd7bc8e7f423cb5285b2",
                      "filename": "seller-288138-barkod-bazlı-iade-raporu-2023.05.03-18.19.04.xlsx",
                      "fullPathName": "/home/rontest/public_html/uploads/64a7fd7bc8e7f423cb5285b2/1688731195_jaTuvvO7dSrFzcJy.xlsx",
                      "type": "excel",
                      "createdAt": "2023-07-07T11:59:55.000000Z",
                      "updatedAt": "2023-07-07T11:59:55.000000Z"
                  },
                  {
                      "id": 59,
                      "userId": "auth0|64a7fd7bc8e7f423cb5285b2",
                      "filename": "Ağrı Dağı Efsanesi .pdf",
                      "fullPathName": "/home/rontest/public_html/uploads/64a7fd7bc8e7f423cb5285b2/1688731130_74bI3pGXa9yq7Fda.pdf",
                      "type": "pdf",
                      "createdAt": "2023-07-07T11:58:50.000000Z",
                      "updatedAt": "2023-07-07T11:58:50.000000Z"
                  }
              ]
              },
              "page": {
                  "links": {
                      "first": true,
                      "last": true,
                      "prev": null,
                      "next": null
                  },
                  "meta": {
                      "current_page": 1,
                      "from": 1,
                      "to": 2,
                      "per_page": 25,
                      "total": 2
                  }
              }       
      }

responseError()
  • Use if the request is error.
    • Parameters optional. setMessage and setStatusCode can be added.

    • Usage examples:

          $this->setMessage('Failed')
          ->setStatusCode(400)
          ->responseError(); 
          $this->responseError(); //default message and status code
          $this->setMessage('Failed!')
          ->responseSuccess();
    • Response example:

      {
          "result": false,
          "message": "Failed",
      }

responseValidation()
  • Use if there is a validation error.
    • Parameter required. setValidation should be added.

    • Usage example:

          $this->setValidation($validation)
          ->responseValidation(); 
    • Response example:

      {
          "result": false,
          "validation_error": {
              "name": [
                  "The name field is required."
              ]
          }
      }

responseNotFound()
  • Use if not found error.
    • Parameter optional. setMessage can be added.

    • Usage examples:

          $this->setMessage('Failed')
          ->responseNotFound(); 
          $this->responseNotFound(); 
    • Response example:

      {
          "result": false,
          "error": "Failed"
      }

responseForbidden()
  • Use in forbidden error.
    • Parameter optional. setMessage can be added.

    • Usage examples:

          $this->setMessage('No Permission')
          ->responseForbidden(); 
          $this->responseForbidden(); 
    • Response example:

      {
          "result": false,
          "error": "No access permission."
      }

responseUnauthorized()
  • Use in unauthorized error.
    • Parameter optional. setMessage can be added.

    • Usage examples:

          $this->setMessage('No Authorized')
          ->responseUnauthorized(); 
          $this->responseUnauthorized(); 
    • Response example:

      {
          "result": false,
          "error": "Not authorized."
      }

responseTryCatch()
  • Use in try-catch error.
    • *Parameter required. setException should be added and setStatusCode can be added. *

    • Usage examples:

          $this->setException($e)
          ->responseTryCatch(); 
    • Response example:

      {
          "result": false,
          "error": "sql connection error"
      }
    • Parameter optional. setMessage can be added.

    • Usage examples:

          $this->setMessage('Failed')
          ->responseValidation(); 
          $this->responseValidation(); 
    • Response example:

      {
          "result": false,
          "error": "Failed"
      }

responseBadRequest()
  • Use if the sent request is incorrect.
    • Parameter optional. setMessage can be added.

    • Usage examples:

          $this->setMessage('Failed')
          ->responseBadRequest(); 
          $this->responseBadRequest(); 
    • Response example:

      {
          "result": false,
          "error": "Bad request."
      }

responseConflict()
  • Use if a mismatch occurs due to a predetermined rule or version differences on the web server of your request.
    • Parameter optional. setMessage can be added.

    • Usage examples:

          $this->setMessage('Failed')
          ->responseConflict(); 
          $this->responseConflict(); 
    • Response example:

      {
          "result": false,
          "error": "Conflict."
      }

responsePayloadTooLarge()
  • Use if the request entity is much larger than the limits defined by the server.
    • Parameter optional. setMessage can be added.

    • Usage examples:

          $this->setMessage('Failed')
          ->responsePayloadTooLarge(); 
          $this->responsePayloadTooLarge(); 
    • Response example:

      {
          "result": false,
          "error": 'Payload too large.'
      }

responseTooManyRequests()
  • Use if the website exceeded the specified request limit.
    • Parameter optional. setMessage can be added.

    • Usage examples:

          $this->setMessage('Failed')
          ->responseTooManyRequests(); 
          $this->responseTooManyRequests(); 
    • Response example:

      {
          "result": false,
          "error": "Too many requests."
      }

responseInternalServer()
  • Use if a server-side error occurred.
    • Parameter optional. setMessage can be added.

    • Usage examples:

          $this->setMessage('Failed')
          ->responseInternalServer(); 
          $this->responseInternalServer(); 
    • Response example:

      {
          "result": false,
          "error": 'Internal server error.'
      }

responseNotImplemented()
  • Use if the server does not support the features required to fulfill the request.
    • Parameter optional. setMessage can be added.

    • Usage examples:

          $this->setMessage('Failed')
          ->responseNotImplemented(); 
          $this->responseNotImplemented(); 
    • Response example:

      {
          "result": false,
          "error": 'Not implemented.'
      }

responseInvalidToken()
  • Use if token is invalid.
    • Parameter optional. setMessage can be added.

    • Usage examples:

          $this->setMessage('Failed')
          ->responseInvalidToken(); 
          $this->responseInvalidToken(); 
    • Response example:

      {
          "result": false,
          "error": 'Invalid token.'
      }