This package provides a structured way to handle API responses in Laravel applications. It includes predefined status codes and a middleware to enforce JSON requests.
NOTE suitable for Laravel v11+
You can install the package via composer:
composer require zerkxubas/api-response-laravel
To publish the configuration file, run the following command:
php artisan vendor:publish --tag="apiresponse"
This will create a config/apiresponse.php file in your project where you can customize the package settings.
The package includes a set of predefined status codes which can be accessed as constants:
use Zerkxubas\ApiResponseLaravel\StatusCode;
$status = StatusCode::OK;
The package provides a middleware to ensure that all API requests expect a JSON response. To use register it in your bootstrap/app.php file.
// use this namespace.
use Zerkxubas\ApiResponseLaravel\Http\Middleware\RequiresJsonMiddleware;
// Add this line inside ->withMiddleware(){}
$middleware->api()->append(RequiresJsonMiddleware::class);
To use the ApiResponseController
provided by this package, you can extend it in your own controllers. This will allow you to use the standardized API response methods.
use Zerkxubas\ApiResponseLaravel\Http\Controllers\ApiResponseController;
class MyCustomController extends ApiResponseController
{
// send response
return $this->sendResponse($data, $message, $code)
// send errorResponse
return $this->sendError($error, $errorMessages, $code);
}
More Example SendResponse
// Example of sending a success response
$data = ['key' => 'value'];
return $this->sendResponse('Data retrieved successfully.', $data, 200);
// Or
return $this->sendResponse('Data retrieved successfully.', $data, StatusCode::OK);
More Example SendErrorResponse
// Example of sending an error response
$errorMessages = [
'error_detail' => 'Customized errors in bulk.'
];
return $this->sendError('File not found.', $errorMessages, 404);
// Or
return $this->sendError('File not found.', $errorMessages, StatusCode::NOT_FOUND);
-
200
StatusCode::OK
-
201
StatusCode::CREATED
-
202
StatusCode::ACCEPTED
-
400
StatusCode::BAD_REQUEST
-
401
StatusCode::UNAUTHORIZED
-
403
StatusCode::FORBIDDEN
-
404
StatusCode::NOT_FOUND
-
500
StatusCode::SERVER_ERROR
You can customize the API response structure by specifying your own implementation of the ApiResponseInterface
in the config/apiresponse.php
configuration file.
At First, create your custom class implementing ApiResponseInterface
:
<?php
namespace App\Services;
use Zerkxubas\ApiResponseLaravel\Contracts\ApiResponseInterface;
class CustomApiResponse implements ApiResponseInterface
{
public function sendResponse($message, $data = [], $code = 200)
{
// Your custom response structure
}
public function sendError($message, $data = [], $code = 400)
{
// Your custom error structure
}
}
Then, update the config/apiresponse.php
file to use your custom class:
return [
'implementation' => App\Services\CustomApiResponse::class,
];
- Open Source, free to use & distribute.