Download the PHP package surazdott/api-response without Composer
On this page you can find all versions of the php package surazdott/api-response. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download surazdott/api-response
More information about surazdott/api-response
Files in surazdott/api-response
Package api-response
Short Description Laravel package for HTTP JSON response for API.
License MIT
Informations about the package api-response
Laravel API Response
Laravel API Response package simplifies the process of generating standardized JSON responses in Laravel applications. It provides a consistent and intuitive API through the package, offering a variety of methods to manage different types of HTTP responses effectively.
Installation
Requires PHP 8.1+
To install the package, you can use Composer:
You can publish the config, languages and resources from with the help of command.
Basic usage
After installing the package, you can use the Api facade or helper function to generate JSON responses in your controllers or anywhere within your application. The following methods are available:
Facade
Generates a generic JSON response with facade.
Helper function
Generates a generic JSON response with helper function.
This is the result.
Methods
response
Generates a generic JSON response with a customizable status code.
response(string $message, mixed $data = [], int $status = 200)
success
Method for a successful operation with HTTP status code 200.
success(string $message, mixed $data = [])
paginate
Return for a successful operation with HTTP paginated data.
paginate(string $message, mixed $data = [])
created
Returns a response indicating that a resource has been successfully created with HTTP status code 201.
created(string $message, mixed $data = [])
error
Returns an error response with HTTP status code 4xx.
error(string $message, int $status = 400)
unauthorized
Returns an unauthorized response with HTTP status code 401.
unauthorized(string $message)
forbidden
Returns an unauthorized response with HTTP status code 401.
forbidden(string $message)
notFound
Returns a not found response with HTTP status code 404.
notFound(string $message)
notAllowed
Returns a method not allowed response with HTTP status code 405.
notAllowed(string $message)
validation
Generates a response indicating validation errors with HTTP status code 400.
validation(string $message, mixed $errors = [])
serverError
Returns an error response with HTTP status code 4xx.
serverError(string $message, int $status = 500)
Note: API response messages are predefined and can be changed from parameters or from the language file.
Request Validation
Laravel's request validation can be used for both web and API. You can call the trait
SurazDott\ApiResposne\Concerns\HasApiResponse;
API Exceptions
If you want to throw the custom exceptionl, you can use the following classes:
ApiResponseException(string $message, ?int status)
ApiValidationException(mixed $errors, ?string $message)
Contributing
If you find any issues or have suggestions for improvements, feel free to open an issue or create a pull request. Contributions are welcome!
License
This package is open-sourced software licensed under the MIT license.