pickmap/response

A Responder for all REST FULL api outputs

0.2.0 2023-09-04 10:04 UTC

This package is not auto-updated.

Last update: 2025-06-09 18:45:08 UTC


README

Most of the time we need the same output structure for all REST FULL API responses.

In this package, based on the rules of the link below (jsend), you can send your response in a standard way https://github.com/omniti-labs/jsend

TypeDescriptionRequired KeysOptional Keys
successAll went well, and (usually) some data was returned.status, data
failThere was a problem with the data submitted, or some pre-condition of the API call wasn't satisfiedstatus, data
errorAn error occurred in processing the request, i.e. an exception was thrownstatus, messagecode, data

How to use this package ?

step #1

Install the package with the following command

composer require pickmap/response

step #3

Go to this path in your Laravel project app/Exceptions/Handler.php and put this codes

use Pickmap\Responder\Res;
public  function  render($request, Throwable  $e)
{
	if ($e  instanceof  ModelNotFoundException)
	{
		return  Res::error('not found',null,404);
	}
	elseif ($e  instanceof  ValidationException)
	{
		return  Res::error($e->getMessage(),null,422);
	}
	
	return  parent::render($request, $e);
}

step #3

now you can use like this

Res::success($objectData);
Res::success($arrayData,201);
Res::error('create faild');
Res::error('new error',419);
Res::fail($data);
Res::response($status,$message,$data,$code);