1. Go to this page and download the library: Download miladev/api-response 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/ */
miladev / api-response example snippets
namespace App\Http\Controllers;
use Miladev\ApiResponse\ApiResponse;
class TestController extends Controller
{
use ApiResponse;
public function successResponseTest()
{
$data = [
'name' => 'milad',
'job' => 'dev',
];
// returns JSON with status=200 by default
return $this->successResponse(data: $data, message: 'OK', statusCode: 200);
}
public function failResponseTest()
{
// returns JSON error with 404 status
return $this->failResponse(message: 'Not found', statusCode: 404);
}
}
use Miladev\ApiResponse\ApiResponse;
class MyApiController
{
use ApiResponse;
// Example: 'status' => self::STATUS_SUCCESS,
'message' => $message,
'meta' => [
'version' => '1.0',
'timestamp' => time(),
],
'result' => $data,
];
}
// Optionally override formatErrorPayload similarly to
use Miladev\ApiResponse\ApiResponse;
class PaginatedController
{
use ApiResponse;
protected function prepareHeaders(array $headers): array
{
// Call parent to normalize and ensure Content-Type
$headers = parent::prepareHeaders($headers);
// Add pagination metadata into headers
$headers['X-Total-Count'] = '123';
$headers['X-Per-Page'] = '25';
return $headers;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.