1. Go to this page and download the library: Download gabylis/api-foundation 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 Gabylis\ApiFoundation\Controllers\ApiBaseController;
use OpenApi\Attributes as OA;
#[OA\Tag(name: 'Products', description: 'Product management')]
class ProductApiController extends ApiBaseController
{
#[OA\Get(
path: '/products',
operationId: 'get-products',
summary: 'List all products',
tags: ['Products'],
security: [['sanctum' => []]],
responses: [
new OA\Response(response: 200, description: 'Products retrieved successfully'),
]
)]
public function index(): JsonResponse
{
$products = Product::paginate(15);
return $this->sendPaginatedResponse($products, 'Products retrieved', ProductResource::class);
}
public function show(int $id): JsonResponse
{
$product = Product::findOrFail($id);
return $this->sendResponse(new ProductResource($product), 'Product retrieved');
}
public function destroy(int $id): JsonResponse
{
Product::findOrFail($id)->delete();
return $this->sendSuccess('Product deleted');
}
}
use Gabylis\ApiFoundation\Requests\ApiFormRequest;
class StoreProductRequest extends ApiFormRequest
{
public function authorize(): bool { return true; }
public function rules(): array
{
return [
'name' => '