1. Go to this page and download the library: Download ajz/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/ */
ajz / api-response example snippets
use Ajz\ApiResponse\Traits\ApiResponseHelpers;
class ApiController extends Controller
{
use ApiResponseHelpers;
public function index()
{
return $this->respondWithSuccess(['data' => $users]);
}
}
// Return 200 with default success response
return $this->respondWithSuccess();
// Return 200 with custom data
return $this->respondWithSuccess(['data' => $users]);
// Return 200 with simple message
return $this->respondOk('Operation completed successfully');
// Return 201 for resource creation
return $this->respondCreated(['id' => $user->id]);
// Return 204 for no content
return $this->respondNoContent();
// Return 404 Not Found
return $this->respondNotFound('User not found');
// Return 401 Unauthorized
return $this->respondUnAuthenticated('Please login');
// Return 403 Forbidden
return $this->respondForbidden('Not allowed');
// Return 400 Bad Request
return $this->respondError('Invalid parameters');
// Return 422 Validation Error
return $this->respondFailedValidation('Validation failed');
// In your constructor or middleware
$this->setDefaultSuccessResponse(['status' => 'ok']);
// Now respondWithSuccess() will use this format
return $this->respondWithSuccess(); // Returns: {"status": "ok"}
// Using with Laravel Collection
$users = User::all();
return $this->respondWithSuccess($users);
// Using with API Resource
$resource = UserResource::make($user);
return $this->respondWithSuccess($resource);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.