PHP code example of zerkxubas / api-response-laravel
1. Go to this page and download the library: Download zerkxubas/api-response-laravel library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
zerkxubas / api-response-laravel example snippets
use Zerkxubas\ApiResponseLaravel\StatusCode;
$status = StatusCode::OK;
// use this namespace.
use Zerkxubas\ApiResponseLaravel\Http\Middleware\RequiresJsonMiddleware;
// Add this line inside ->withMiddleware(){}
$middleware->api()->append(RequiresJsonMiddleware::class);
// 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);
// 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);
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
}
}