<?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
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.