Giter Club home page Giter Club logo

laravel-responder's Introduction

Laravel Responder

StyleCI Total Downloads

A tiny Laravel package to format API responses.

Installation

Via Composer

$ composer require gaurav93d/laravelresponder

For Laravel version < 5.5, consider listing service provider and facade in config/app.php

'providers' => [
    ...
    gaurav93d\LaravelResponder\LaravelResponderServiceProvider::class,
    ...
],
'aliases' => [
    ...
    'Responder' => gaurav93d\LaravelResponder\Facades\Responder::class,
    ...
],

Usage

Response format

{
  "success": true,
  "status": 200,
  "data": [
    "Here your beautiful data goes"
  ],
  "errors": [
    "Here your ugly errors go"
  ]
}

Send success

...
use gaurav93d\LaravelResponder\Facades\Responder;
...

Responder::success($data  = [], $status = 200);

Send errors

...
use gaurav93d\LaravelResponder\Facades\Responder;
...

// Multiple errors
return Responder::errors($errors  = [], $status = 200);

// Single error
return Responder::error($message = 'Error!', $status = 200);

// Validation errors
return Responder::respondValidationErrors(Validator $validator);

// Here are some comman error responses ๐Ÿ‘‡

// Internal server error
return Responder::respondInternalError($message = 'Internal Error!'); // status = 500

// Unauthorized error
return Responder::respondUnauthorizedError($message = 'Unauthorized!'); // status = 401

// Bad request error
return Responder::respondBadRequestError($message = 'Bad Request!'); // status = 400

// Not found error
return Responder::respondNotFoundError($message = 'Not found!'); // status = 404

Attach response headers

...
use gaurav93d\LaravelResponder\Facades\Responder;
...

return Responder::headers($headers = [])->success($data  = [], $status = 200);
...
return Responder::headers($headers = [])->errors($errors  = [], $status = 200);
...

... ๐Ÿ˜‰ Stay tuned! More features to come.

Change log

Please see the changelog for more information on what has changed recently.

Credits

License

MIT. Please see the license file for more information.

laravel-responder's People

Contributors

93gaurav93 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.