PHP code example of zero-to-prod / laravel-openapi

1. Go to this page and download the library: Download zero-to-prod/laravel-openapi 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/ */

    

zero-to-prod / laravel-openapi example snippets


use Illuminate\Http\JsonResponse;
use ZeroToProd\LaravelOpenapi\ApiSchema;

class ShowArticleController
{
    #[ApiSchema([
        'paths' => [
            '/articles/{id}' => [
                'get' => [
                    'operationId' => 'showArticle',
                    'parameters' => [
                        [
                            'name' => 'id',
                            'in' => 'path',
                            '    'type' => 'object',
                                        'sonResponse
    {
        return new JsonResponse(
            data: ['id' => $id, 'title' => 'Zero to prod'],
            headers: ['Content-Type' => 'application/vnd.api+json'],
        );
    }
}

#[ApiSchema(static function (): array {
    return [
        'paths' => [
            '/articles/{id}' => [
                'get' => [
                    'operationId' => 'showArticle',
                    'responses' => ['200' => ArticleResource::responseSchema()],
                ],
            ],
        ],
    ];
})]
public function __invoke(string $id): JsonResponse

#[ApiSchema(static fn (): array => [...])] // Fatal error: Constant expression contains invalid operations

// config/openapi.php
'route' => ['enabled' => false, /* ... */],

// routes/api.php
use ZeroToProd\LaravelOpenapi\ApiSchema;

Route::middleware('auth:sanctum')
    ->prefix('internal')
    ->group(fn () => ApiSchema::routes());

ApiSchema::routes('docs/openapi.json', 'docs.schema')->middleware('throttle:60,1');

use ZeroToProd\LaravelOpenapi\ValidatesSchema;

abstract class TestCase extends BaseTestCase
{
    use ValidatesSchema;
}

$this->assertMatchesSchema($this->getJson('articles/42'));
bash
php artisan openapi:install
bash
php artisan vendor:publish --tag=openapi-config
bash
php artisan openapi:validate
bash
php artisan openapi:coverage --reset && vendor/bin/pest && php artisan openapi:coverage
bash tab=Codex
codex mcp add laravel-openapi -- php "artisan" "mcp:start" "laravel-openapi"