1. Go to this page and download the library: Download ayd/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/ */
use Ayd\ApiResponseLaravel\ApiResponse;
class UserController
{
public function index(ApiResponse $response)
{
return $response->success($users);
}
public function store(ApiResponse $response)
{
// validation...
return $response->created($user, 'User created');
}
public function show(ApiResponse $response, $id)
{
$user = User::find($id);
if (!$user) {
$response->notFound('User not found'); // throws HttpResponseException
}
return $response->success($user);
}
}
use Ayd\ApiResponseLaravel\ApiResponseTrait;
class UserController
{
use ApiResponseTrait;
public function index()
{
return $this->success(User::all());
}
public function store()
{
// validation...
return $this->created($user, 'User created');
}
public function destroy($id)
{
User::findOrFail($id)->delete();
return $this->noContent();
}
}
use Ayd\ApiResponseLaravel\Facades\Response;
Route::get('/health', fn () => Response::ok(['status' => 'healthy']));
public function show(ApiResponse $response, $id)
{
$user = User::find($id);
if (!$user) {
$response->notFound('User not found');
// execution stops here — no return needed
}
return $response->success($user);
}
public function index(ApiResponse $response)
{
$paginator = User::paginate(20);
return $response->paginator($paginator, UserResource::class);
}
use Ayd\ApiResponseBase\Contracts\AbilitiesResolver;
$this->app->bind(AbilitiesResolver::class, MyAbilitiesResolver::class);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.