GithubHelp home page GithubHelp logo

laravel-api-responses's Introduction

Laravel API Responses

Latest Stable Version Total Downloads Build status

A very small package which helps you to easily returning readable API responses.

Installation

Install the package via Composer.

composer require dees040/laravel-api-responses

You're ready to go!

Usage

Just use one of the helper functions and you're good to go.

<?php

namespace App\Http\Controllers;

use App\User;
use App\Http\Controllers\Controller;

class UsersController extends Controller
{
    /**
     * Show the given user.
     *
     * @param  \App\User  $user
     * @return \Illuminate\Http\Response
     */
    public function show(User $user)
    {
        if (! $user->isAdmin()) {
            return forbidden();
        }
        
        return ok($user);
    }
}

Methods

All methods accept a $data parameter. This can be any data which can be used in a JSON response, such as strings, integers, arrays, models, etc..

Method Status Code
ok($data) 200
created($data) 201
accepted($data) 202
no_content() 204
bad_request($message, $errors) 400
unauthenticated($message, $errors) 401
forbidden($message, $errors) 403
not_found($message, $errors) 404
method_not_allowed($message, $errors) 405
not_acceptable($message, $errors) 406
teapot($message, $errors) 418
unprocessable_entity($message, $errors) 422

Custom response (code)

If you'd wish to send a status code which is not in the list you could use the json_response($data = null, $status = 200) helper function. Here you can find a cheat sheet for HTTP status codes or use my personal favorite http.cat ๐Ÿ˜‰.

If you want to send error response you can use the error_json_response($message = '', $errors = [], $status = 400). This method will send an json response like this:

error_json_response('User not found.', [
    'id' => 'The ID does not exists.'
]);

Output:

{
  "message": "User not found.",
  "errors": {
    "id": "The ID does not exists."
  }
}

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.