- Overview
- Features
- Installation
- Usage
- Response Structure
- Configuration
- Error Handling
- Testing
- Contributing
- License
- Support
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.
- 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.
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"
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.
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."
}
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 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);
}
}
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',
]);
}
We welcome contributions to improve Laravel API Responder. To contribute:
- Fork the repository.
- Create a new branch.
- Make your changes.
- Submit a pull request.
Please ensure your code adheres to our coding standards and includes tests where applicable.
This package is licensed under the MIT License. See the LICENSE file for more information.
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.