Installation:
composer require wotz/laravel-swagger-ui
php artisan swagger-ui:install
This publishes the config (config/swagger-ui.php) and service provider (app/Providers/SwaggerUiServiceProvider.php).
Configure Swagger File:
Ensure your OpenAPI spec (JSON/YAML) is accessible. The package defaults to public/swagger.json or public/swagger.yaml.
Update the path in config/swagger-ui.php if needed:
'path' => 'public/swagger.json',
First Use Case:
Visit /swagger in your browser. The UI will auto-detect your Laravel environment (e.g., APP_URL) and configure the API base URL dynamically.
Example: If APP_URL=http://localhost:8000, the Swagger UI will use http://localhost:8000/api as the base path.
Environment-Aware API Docs:
APP_URL into Swagger UI, ensuring docs always reflect the current environment (dev/staging/prod).'base_url' => env('SWAGGER_BASE_URL', config('app.url')),
OAuth2 Integration:
config/swagger-ui.php:
'oauth2' => [
'client_id' => 'your-client-id',
'client_secret' => 'your-client-secret',
'auth_url' => 'https://your-auth-server/oauth/authorize',
'token_url' => 'https://your-auth-server/oauth/token',
],
Dynamic Swagger File Loading:
Route::get('/swagger.json', function () {
return response()->json($this->generateOpenApiSpec());
});
Middleware Integration:
SwaggerUiServiceProvider.php:
public function boot()
{
$this->routes(function () {
Route::middleware(['auth:sanctum'])->group(function () {
Route::get('/swagger', [SwaggerUiController::class, 'index']);
});
});
}
Theming and Assets:
php artisan vendor:publish --tag=swagger-ui-views
resources/views/vendor/swagger-ui/index.blade.php to inject custom CSS/JS.API Versioning:
// routes/api.php
Route::prefix('v1')->group(function () {
Route::get('/swagger.json', [SwaggerController::class, 'v1']);
});
config/swagger-ui.php:
'paths' => [
'v1' => 'api/v1/swagger.json',
'v2' => 'api/v2/swagger.json',
],
Conditional UI Loading:
'enabled' => !app()->environment('production'),
Webhook Integration:
route:list events):
Route::macro('generateSwagger', function () {
$spec = $this->generateSpec();
file_put_contents(public_path('swagger.json'), json_encode($spec));
});
CORS Issues:
localhost).Header::set('Access-Control-Allow-Origin', '*');
Environment Mismatch:
APP_URL by default. If your API is behind a proxy (e.g., Nginx), set:
'base_url' => env('API_BASE_URL', config('app.url')),
OAuth2 Token Expiry:
// Override in custom JS (e.g., via blade injection)
window.SwaggerUIBundle.prototype.updateToken = function() { ... };
File Permissions:
chmod 644 public/swagger.json
Validate Spec:
Check Network Requests:
F12) and verify the Swagger UI fetches the correct spec URL (e.g., /swagger.json).Log Configuration:
SwaggerUiServiceProvider.php:
\Log::debug('Swagger UI config:', config('swagger-ui'));
Clear Published Assets:
rm -rf resources/views/vendor/swagger-ui
php artisan vendor:publish --tag=swagger-ui-views --force
Custom Controllers:
SwaggerUiController to add logic (e.g., auth checks):
namespace App\Http\Controllers;
use Wotzebra\SwaggerUi\Http\Controllers\SwaggerUiController as BaseController;
class SwaggerUiController extends BaseController {
public function index() {
if (!auth()->check()) abort(403);
return parent::index();
}
}
SwaggerUiServiceProvider.php to bind the custom controller.Dynamic Spec Generation:
RouteServiceProvider to generate specs on-the-fly:
public function boot()
{
$this->routes(function () {
Route::get('/swagger.json', function () {
return response()->json($this->generateSpecFromRoutes());
});
});
}
Plugin Integration:
'plugins' => [
'swagger-ui-bundle/plugin/swagger-ui-standalone-preset.min.js',
'swagger-ui-bundle/plugin/swagger-ui-oauth2-grant.min.js',
],
Localization:
php artisan vendor:publish --tag=swagger-ui-translations
resources/lang/vendor/swagger-ui/en.json.How can I help you explore Laravel packages today?