Home

Awesome

API Responder - Laravel Response

ars/api-responder-laravel is a Laravel package designed to streamline the creation of standardized API responses. It provides a user-friendly interface for managing API responses, including common status codes and error messages, making API development in Laravel more efficient and consistent.

Features

Installation

You can install the library via Composer. Run the following command:

composer require ars/api-responder-laravel

Usage

Basic Response

You can use the Responder facade to create standardized API responses.

Example:

use Responder;

return Responder::ok($response);
return Responder::setMessage('success')->ok($response);
return Responder::tooManyRequests();
return Responder::notFound();
return Responder::unAuthorized();

$data = $this->repository->paginate();
$links = [
    'next' => $data->nextPageUrl(),
    'prev' => $data->previousPageUrl(),
    'path' => $data->path(),
];
$meta = [
    'total'        => $data->total(),
    'current_page' => $data->currentPage(),
    'per_page'     => $data->perPage(),
];
return Responder::setMeta($meta)->setLinks($links)->ok($data->items());

return Responder::setStatusCode(500)->setMessage($e->getMessage())->respond();

{
  "message": "Operation successful!",
  "success": true,
  "data": {
    "accessToken": "28|PDVEA7z6mUPcbbIybkpiMPJMvy3TLtuWOguiGDGn13c67491",
    "tokenType": "Bearer",
    "expiresIn": null
  }
}

Additional Methods

Messages

The library supports various status messages:

If a message key is provided, use setMessage(string $message) to set the message.

Contributing

Contributions are welcome! Please adhere to the following guidelines:

  1. Fork the repository.
  2. Create a feature branch (git checkout -b feature/my-new-feature).
  3. Commit your changes (git commit -am 'Add new feature').
  4. Push to the branch (git push origin feature/my-new-feature).
  5. Open a Pull Request.

License

This library is licensed under the MIT License.

Contact

For support or questions, please open an issue on GitHub.