PHP code example of yoosuf / laravel-api
1. Go to this page and download the library: Download yoosuf/laravel-api 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/ */
yoosuf / laravel-api example snippets
'api' => [
\Yoosuf\LaravelApi\Http\Middleware\ForceJsonMiddleware::class,
\Yoosuf\LaravelApi\Http\Middleware\RequestIdMiddleware::class,
\Yoosuf\LaravelApi\Http\Middleware\SecurityHeadersMiddleware::class,
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'laravel-api.force-json',
'laravel-api.request-id',
'laravel-api.security-headers',
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
app(\Yoosuf\LaravelApi\Exceptions\ApiExceptionRenderer::class)->register();
use Yoosuf\LaravelApi\Concerns\HasApiResponses;
class OrderController extends Controller
{
use HasApiResponses;
public function index(): JsonResponse
{
$orders = Order::paginate(20);
return $this->fromPaginator($orders);
}
public function store(StoreOrderRequest $request): JsonResponse
{
$order = Order::create($request->validated());
return $this->created($order, 'Order created', null, route('orders.show', $order));
}
public function show(Order $order): JsonResponse
{
$response = $this->success($order);
return $this->withEtag($response);
}
public function destroy(Order $order): JsonResponse
{
$order->delete();
return $this->noContent();
}
}
use Yoosuf\LaravelApi\Facades\ApiResponse;
// Facade
return ApiResponse::success($data, 'Fetched');
return ApiResponse::fromPaginator($paginator);
return ApiResponse::validation('Invalid input', $errors);
// Global helpers
return response_success($data, 'Fetched');
return response_failed('Bad input', 422, $errors);
return api_paginated($items, $total, $nextUrl);
return $this->structuredError('ResourceNotFound', 'Order not found', 404, 'orderId');
$response = $this->success($data);
$response = $this->withEtag($response); // adds ETag header
return $this->checkEtag($request, $response); // returns 304 if If-None-Match matches
// Kernel.php api group
'laravel-api.force-json',
Route::middleware(['laravel-api.deprecation:2025-01-01,2026-01-01'])->group(function () {
// deprecated routes
});
// AppServiceProvider::boot()
app(\Yoosuf\LaravelApi\Exceptions\ApiExceptionRenderer::class)->register();
// config/laravel-api.php
'providers' => [
App\OpenApi\Providers\OrderSchemaProvider::class,
],
class OrderSchemaProvider implements SchemaProvider
{
public function schemas(): array
{
return [
'Order' => [
'type' => 'object',
'properties' => [
'id' => ['type' => 'integer'],
'status' => ['type' => 'string', 'enum' => ['draft', 'placed', 'fulfilled']],
],
],
];
}
}
'overrides' => [
'routes' => [
'orders.store' => ['post' => ['summary' => 'Place an order', 'tags' => ['orders']]],
],
'actions' => [
'App\Http\Controllers\OrderController@store' => [
'post' => ['x-internal' => true],
],
],
],
use Yoosuf\LaravelApi\Testing\ApiResponseAssertions;
class OrderTest extends TestCase
{
use ApiResponseAssertions;
public function test_index(): void
{
$response = $this->getJson('/api/orders');
$this->assertApiPaginated($response);
}
public function test_store_validation(): void
{
$response = $this->postJson('/api/orders', []);
$this->assertApiValidationError($response, 'amount');
}
public function test_store_success(): void
{
$response = $this->postJson('/api/orders', ['amount' => 100]);
$this->assertApiCreated($response);
$this->assertApiDataKey($response, 'id', 1);
}
public function test_rate_limiting(): void
{
$response = $this->getJson('/api/orders');
$this->assertApiTooManyRequests($response, 60); // asserts Retry-After: 60
}
}
'docs_ui' => [
'enabled' => true,
'driver' => 'swagger',
'route' => 'api-docs',
'title' => 'Laradoc API Reference',
'spec_url' => '/openapi.json',
'middleware' => [],
],
return response_success(['user' => $user], 'Fetched');
return response_failed('Validation failed', 422, ['email' => ['bash
php artisan api:openapi
php artisan api:openapi --format=json
php artisan api:openapi --format=yaml --output=docs/openapi.v2.yaml
php artisan api:openapi --prefix=/api/v1
php artisan api:openapi --bash
php artisan vendor:publish --tag=laravel-api-config
bash
php artisan vendor:publish --tag=laravel-api-assets
bash
php artisan api:openapi
bash
php artisan api:openapi --format=json
bash
php artisan api:openapi --format=yaml --output=docs/openapi.v1.yaml
bash
php artisan api:openapi --prefix=/api/v1
bash
php artisan vendor:publish --tag=laravel-api-assets --force
bash
php artisan api:openapi --format=all --prefix=/api/v1