Skip to content

A fluent helper and facade to ensure consistent, idempotent API responses in Laravel and Lumen

License

Notifications You must be signed in to change notification settings

myerscode/laravel-api-response

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel API Response

A fluent helper to provide a consistent shaped API responses in Laravel

Latest Stable Version Total Downloads License

Why is this package helpful?

This package ensures your API will always return the same envelope shape, so consuming apps always know what to expect!

Install

You can install this package via composer:

composer require myerscode/laravel-api-response

Usage

In a Laravel controller you just to build up your response and return it!

The api() helper return a Response Builder and as it implements the Responsable trait you dont need to do anything more than return the builder

Using the api() helper function

function resource()
{
    return api()->status(201)->data(['name' => 'Foo Bar'])->message('Record Created!');
}

Using a Builder class

function resource() {
    $buillder = new Builder();
    $builder->status(201)->data(['name' => 'Foo Bar'])->message('Record Created!');
    return $builder;
}

Would return the following JSON response.

{
    "status": 201,
    "data": {
        "name": "Foo Bar"
    },
    "meta": [],
    "messages": [
        "Record Created!"
    ]
}

License

The MIT License (MIT). Please see License File for more information.

About

A fluent helper and facade to ensure consistent, idempotent API responses in Laravel and Lumen

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages