PHP code example of gabylis / api-foundation

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/ */

    

gabylis / api-foundation example snippets


#[OA\Info(
    title: 'My API',
    version: '1.0.0',
    description: 'My API description',
    contact: new OA\Contact(email: '[email protected]')
)]
#[OA\Server(url: '/api', description: 'Local')]
#[OA\SecurityScheme(
    securityScheme: 'sanctum',
    type: 'http',
    scheme: 'bearer'
)]
class OpenApiInfo {}

'annotations' => [
    base_path('app'),
],

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'  => '
bash
php artisan vendor:publish --tag=api-foundation-openapi
bash
php artisan l5-swagger:generate
bash
php artisan l5-swagger:generate
bash
php artisan make:api-controller ProductApiController
php artisan make:api-controller ProductApiController --resource=products --tag="Products"
bash
# routes/api.php
Route::apiResource('products', ProductApiController::class);

php artisan l5-swagger:generate
bash
# 1. Install packages
composer p artisan vendor:publish --provider "L5Swagger\L5SwaggerServiceProvider"

# 3. Publish OpenAPI info file
php artisan vendor:publish --tag=api-foundation-openapi
# → Edit app/OpenApi/OpenApiInfo.php

# 4. Set annotations path in config/l5-swagger.php
# 'annotations' => [ base_path('app') ]

# 5. Generate a documented controller
php artisan make:api-controller ProductApiController --resource=products --tag="Products"

# 6. Add route
# Route::apiResource('products', ProductApiController::class);

# 7. Generate docs
php artisan l5-swagger:generate

# 8. Open http://localhost:8000/api/documentation
bash
php artisan vendor:publish --tag=api-foundation-stubs