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: 'admin-api', // <- Use spec other than default
       skipResponseValidation: true // <- Skip Response Validation
     ));
   

   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(
       Request $request,
       \Throwable $error,
       ErrorType $errorType,
     ): Response {
       return new Response(
         match ($errorType) {
           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 openapi-validator:publish