Skip to content

Lightweight Laravel package for clean, standardized API responses. Simplify your web and mobile apps with easy-to-use methods. πŸš€πŸŒŸ

License

Notifications You must be signed in to change notification settings

Creatonshsr/laravel-api-responder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Laravel API Responder: A Consistent API Response Package

GitHub release GitHub Releases

Table of Contents

Overview

Laravel API Responder is a reusable package designed for Laravel applications. It helps developers create consistent API responses for both web and mobile applications. By using this package, you can ensure that your API responses are uniform, making it easier for clients to consume your services.

Features

  • Consistent Responses: Standardizes API responses across your application.
  • Easy Integration: Simple to install and configure within your Laravel project.
  • Customizable: Adjust response formats to meet your specific needs.
  • Error Handling: Streamlined error responses for better client-side handling.
  • Support for JSON API: Fully compatible with JSON API specifications.
  • Documentation: Well-documented with examples to help you get started quickly.

Installation

To install the Laravel API Responder package, use Composer. Run the following command in your terminal:

composer require creatonshsr/laravel-api-responder

Once installed, you can publish the configuration file using:

php artisan vendor:publish --provider="Creatonshsr\LaravelApiResponder\LaravelApiResponderServiceProvider"

Usage

After installation, you can start using the package in your controllers. Here’s a simple example:

use Creatonshsr\LaravelApiResponder\ApiResponder;

class UserController extends Controller
{
    public function index()
    {
        $users = User::all();
        return ApiResponder::success($users);
    }
}

This will return a successful response with the list of users.

Response Structure

The package provides a standard response structure. Here’s what the response looks like:

{
    "status": "success",
    "data": {
        // your data here
    },
    "message": "Request was successful."
}

In case of an error, the response structure will look like this:

{
    "status": "error",
    "message": "An error occurred."
}

Configuration

You can customize the package's behavior by editing the configuration file located at config/api_responder.php. Here are some options you can adjust:

  • Default Status: Set the default status for responses.
  • Custom Messages: Define custom messages for success and error responses.
  • Error Codes: Map specific error codes to your application's needs.

Error Handling

Error handling is simplified with Laravel API Responder. When an exception occurs, you can catch it and return a standardized error response:

use Illuminate\Database\Eloquent\ModelNotFoundException;

public function show($id)
{
    try {
        $user = User::findOrFail($id);
        return ApiResponder::success($user);
    } catch (ModelNotFoundException $e) {
        return ApiResponder::error('User not found', 404);
    }
}

Testing

To ensure that your implementation works as expected, write tests for your API endpoints. Here’s a simple test case:

public function testUserIndexReturnsSuccess()
{
    $response = $this->get('/api/users');
    $response->assertStatus(200);
    $response->assertJsonStructure([
        'status',
        'data',
        'message',
    ]);
}

Contributing

We welcome contributions to improve Laravel API Responder. To contribute:

  1. Fork the repository.
  2. Create a new branch.
  3. Make your changes.
  4. Submit a pull request.

Please ensure your code adheres to our coding standards and includes tests where applicable.

License

This package is licensed under the MIT License. See the LICENSE file for more information.

Support

For support, please visit the Releases section. You can also check the issues page for common problems and solutions.


For more information, visit the Releases section.