wotzebra/
laravel-swagger-ui
g4t.io

Add Swagger UI to Your Laravel API

19 views 282 stars by Paul Redmond

Add Swagger UI to a Laravel application.

README from wotzebra/laravel-swagger-ui Open on GitHub →

Laravel Swagger UI

Latest Version on Packagist Total Downloads

This package makes it easy to make your project's Swagger (OpenAPI v3 JSON or YAML) file accessible in a Swagger UI right in your Laravel application.

The Swagger UI will automatically use your current project environment. It will set your api's base url to the active base url. Possible oauth2 configuration, such as urls and client-id/client-secret, can also be injected in Swagger UI via the configuration file.

Installation

You can install the package via composer:

composer require wotz/laravel-swagger-ui

After installing Laravel Swagger UI, publish its service provider and configuration file using the swagger-ui:install Artisan command.

php artisan swagger-ui:install

Usage

Customize authentication and authorization

The Swagger UI is exposed at /swagger. By default, you will only be able to access it in the local environment. Within your app/Providers/SwaggerUiServiceProvider.php file, there is a gate method. This authorization gate controls access to Swagger UI in non-local environments. You can modify this gate as needed to restrict access to your Swagger UI and Swagger (OpenAPI v3) file:

/**
 * Register the Swagger UI gate.
 *
 * This gate determines who can access Swagger UI in non-local environments.
 *
 * @return void
 */
protected function gate()
{
    Gate::define('viewSwaggerUI', function ($user = null) {
        return in_array(optional($user)->email, [
            //
        ]);
    });
}

Customize locations of Swagger (OpenAPI v3) files

In the published config/swagger-ui.php file, you edit the path to the Swagger UI and the location of the Swagger (OpenAPI v3) file. By default, the package expects to find the OpenAPI file in 'resources/swagger' directory. You can also provide an url if the OpenAPI file is not present in the Laravel project itself. This is also where you can define multiple versions for the same api.

// in config/swagger-ui.php

return [
    // ...

    'path' => 'swagger',

    'versions' => [
        'v1' => resource_path('swagger/openapi.json')
    ],

    // ...
];

Customize modifications to Swagger (OpenAPI v3) files and SwaggerUI

By default the package will customize the server url and the oauth urls in the OpenAPI file to the base url of the Laravel application. This can be disabled in the config.

// in config/swagger-ui.php

return [
    // ...

    'modify_file' => true,

    // ...
];

You can also set what variables that should be customizable for the server url in Swagger UI. Variable must be present with the same key (case-sensitive) in the url surrounded by curly brackets.

// in config/swagger-ui.php

return [
    // ...

    'server_url' => 'http://foo.bar/{Language}/api',

    'server_variables' => ['Language' => ['default' => 'en']],

    // ...
]

You can also set an oauth client ID and client secret. These values will be automatically prefilled in the authentication view in Swagger UI.

// in config/swagger-ui.php

return [
    // ...

    'oauth' => [
        'token_path' => 'oauth/token',
        'refresh_path' => 'oauth/token',
        'authorization_path' => 'oauth/authorize',

        'client_id' => env('SWAGGER_UI_OAUTH_CLIENT_ID'),
        'client_secret' => env('SWAGGER_UI_OAUTH_CLIENT_SECRET'),
    ];

    // ...
];

Enable and configure MCP Server based on Swagger (OpenAPI v3) files

If you want to use the mcp server that returns info about your swagger files, then you need to install the laravel/mcp composer package and enable the swagger server in the config:

// in config/swagger-ui.php

return [
    // ...

    'mcp' => [
        'enabled' => true,
    ],

    // ...
];

By default, the MCP server is protected by the auth:api middleware and the viewSwaggerUI gate. If you are using Laravel Passport, then you still have to enable the MCP OAuth routes by adding Mcp::oauthRoutes() and a Passport authorization view to your codebase.

Testing

composer test

Linting

composer lint

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email gunther.debrauwer@whoownsthezebra.be instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

More from the ecosystem

durable-workflow/
workflow
g4t.io
9 1,292

Durable Workflow: Long-Running Workflows for Laravel

Embedded durable workflow runtime and orchestration engine for Laravel applications.

durable-workflow/workflow via Laravel News
packstub/
session-replay
g4t.io
14 52

Session Replay for Laravel: Self-Hosted Browser Recordings

Session replay for Laravel: record what a person did in the browser with rrweb, keep the recording on your own disk and database, and watch it behind a gate you define — masked inputs by default, errors, failed Livewire requests and web vitals marked on the timeline, a link to the replay in every log line. packstub/filament-session-replay adds the sessions to a Filament panel.

packstub/session-replay via Laravel News
cyberwizard-dev/
schedule-catchup
g4t.io
17

Replay Missed Laravel Scheduled Tasks With Schedule Catchup

Schedule Catchup replays missed Laravel scheduled tasks after downtime, with opt-in recovery, bounded retries, and access to the original scheduled date.

cyberwizard-dev/schedule-catchup via Laravel News