Simplifying API responses in Laravel with kolirt/laravel-api-response package

API responses are a crucial part of any web application, especially when dealing with client-server communications. In Laravel, while handling API responses is straightforward, maintaining a consistent structure across different projects can be chall…


This content originally appeared on DEV Community and was authored by kolirt

Simplifying API responses in Laravel with kolirt/laravel-api-response package

API responses are a crucial part of any web application, especially when dealing with client-server communications. In Laravel, while handling API responses is straightforward, maintaining a consistent structure across different projects can be challenging. This is where the kolirt/laravel-api-response package comes in handy.

What is kolirt/laravel-api-response?
The kolirt/laravel-api-response package is designed to unify API responses for Laravel projects, drawing inspiration from the well-structured responses of Telegram's API. This package simplifies the process of generating consistent API responses, whether you're returning success or error messages.

Key Features

  • Unified Response Structure: The package ensures that all your API responses follow a consistent structure, which is essential for maintainability and clarity.
  • Error and Success Responses: Easily generate error and success responses with dedicated methods.
  • Flexible and Extensible: Customize your responses with methods to set HTTP status codes, descriptions, errors, and more.

Getting Started
To get started with kolirt/laravel-api-response, ensure your project meets the following requirements:

  • PHP version: >=8.1
  • Laravel version: >= 10

You can install the package via Composer:

composer require kolirt/laravel-api-response

Usage Example
Here’s a quick example of how you can use the kolirt/laravel-api-response package:

Error response

class ExampleController extends Controller
{
    public function index()
    {
        return api()
            ->error(404)
            ->setDescription('Resource not found')
            ->render();
    }

}

Json response

{
    "ok": false,
    "error_code": 404,
    "description": "Resource not found"
}

Validation error response

class ExampleController extends Controller
{
    public function index()
    {
        return api()
            ->error(422)
            ->setErrors([
                'password' => 'Field is required'
            ])
            ->render();
    }

}

Json response

{
    "ok": false,
    "error_code": 422,
    "errors": {
        "password": "Field is required"
    }
}

Success Response

class ExampleController extends Controller
{
    public function store()
    {
        return api()
            ->success()
            ->setDescription('Resource created successfully')
            ->setData(['id' => 123])
            ->render();
    }

}

Json response

{
    "ok": true,
    "description": "Resource created successfully",
    "data": {
        "id": 123
    }
}

Methods Overview
The package provides a set of methods to customize your API responses:

  • error: Generate an error response.
  • success: Generate a success response.
  • setCode: Set the HTTP status code.
  • setDescription: Add a description to the response.
  • setErrors: Include specific error details.
  • abort: Abort the request with a custom response.
  • cookie: Add cookies to the response.
  • setData: Attach data to the success response.
  • render: Render the final response.

Conclusion
The Laravel Api Response package is a powerful tool for Laravel developers looking to streamline their API response handling. By unifying the structure of responses, it not only improves code readability and consistency but also enhances the overall development experience.

Feel free to explore the package further by visiting its GitHub repository.


This content originally appeared on DEV Community and was authored by kolirt


Print Share Comment Cite Upload Translate Updates
APA

kolirt | Sciencx (2024-08-12T17:16:20+00:00) Simplifying API responses in Laravel with kolirt/laravel-api-response package. Retrieved from https://www.scien.cx/2024/08/12/simplifying-api-responses-in-laravel-with-kolirt-laravel-api-response-package/

MLA
" » Simplifying API responses in Laravel with kolirt/laravel-api-response package." kolirt | Sciencx - Monday August 12, 2024, https://www.scien.cx/2024/08/12/simplifying-api-responses-in-laravel-with-kolirt-laravel-api-response-package/
HARVARD
kolirt | Sciencx Monday August 12, 2024 » Simplifying API responses in Laravel with kolirt/laravel-api-response package., viewed ,<https://www.scien.cx/2024/08/12/simplifying-api-responses-in-laravel-with-kolirt-laravel-api-response-package/>
VANCOUVER
kolirt | Sciencx - » Simplifying API responses in Laravel with kolirt/laravel-api-response package. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/12/simplifying-api-responses-in-laravel-with-kolirt-laravel-api-response-package/
CHICAGO
" » Simplifying API responses in Laravel with kolirt/laravel-api-response package." kolirt | Sciencx - Accessed . https://www.scien.cx/2024/08/12/simplifying-api-responses-in-laravel-with-kolirt-laravel-api-response-package/
IEEE
" » Simplifying API responses in Laravel with kolirt/laravel-api-response package." kolirt | Sciencx [Online]. Available: https://www.scien.cx/2024/08/12/simplifying-api-responses-in-laravel-with-kolirt-laravel-api-response-package/. [Accessed: ]
rf:citation
» Simplifying API responses in Laravel with kolirt/laravel-api-response package | kolirt | Sciencx | https://www.scien.cx/2024/08/12/simplifying-api-responses-in-laravel-with-kolirt-laravel-api-response-package/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.