PHP code example of kentaroutakeda / laravel-openapi-validator

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

    

kentaroutakeda / laravel-openapi-validator example snippets


   Route::get('/example', ExampleController::class)
       ->middleware(OpenApiValidator::class); // <- Add this line
   

   Route::get('/', ExampleController::class)
       ->middleware(OpenApiValidator::config(
           // // Provider name from config
           provider: 'my-provider',
           // For performance, skip response validation in production
           skipResponseValidation: app()->isProduction(),
       ));
   

   class MyResolver implements ResolverInterface
   {
       public function getJson(array $options): string
       {
           // This example assumes that the schema exists in the root directory.
           return File::get(base_path('openapi.json'));
       }
   }
   

   return [
       // Set the provider name.
       'default' => 'my-resolver',

       'providers' => [
           // Set the provider name you created.
           'my-resolver' => [
               // Specify the class you created in the `resolver` parameter.
               'resolver' => MyResolver::class,
           ],
       ],
   ];
   

   class MyErrorRenderer implements ErrorRendererInterface
   {
       public function render(
           \Throwable $error,
           Request $request,
           ?Response $response = null,
       ): Response {
           return new Response(
               match ($response === null) {
                   ErrorType::Request => "Request Error: " . $error->getMessage(),
                   ErrorType::Response => "Response Error: " . $error->getMessage(),
               }
           );
       }
   }
   

   // AppServiceProvider.php

   public function register(): void
   {
       $this->app->bind(
           ErrorRendererInterface::class,
           MyErrorRenderer::class
       );
   }
   
bash
   php artisan openapi-validator:cache
   
bash
   php artisan openapi-validator:clear
   
bash
   php artisan optimize        # Includes openapi-validator:cache
   php artisan optimize:clear  # Includes openapi-validator:clear
   
bash
php artisan openapi-validator:publish