Installation
composer require dedoc/scramble
php artisan vendor:publish --provider="Dedoc\Scramble\ScrambleServiceProvider" --tag="scramble-config"
Run Scramble
php artisan scramble:generate
storage/app/scramble/api.json (default path).Access Docs
/docs/api (UI) or /docs/api.json (raw OpenAPI spec).viewApiDocs gate is defined in app/Providers/AuthServiceProvider.php for non-local environments:
Gate::define('viewApiDocs', fn () => true); // Adjust logic as needed
First Use Case: Quick API Exploration
/docs/api UI to interactively test endpoints, inspect request/response schemas, and validate parameters without manual testing.Automated Documentation Generation
php artisan scramble:generate manually or integrate into CI/CD (e.g., post-deploy).* * * * * php artisan scramble:generate) for nightly updates.
// config/scramble.php
'schedule' => true,
Customizing Documentation
scramble.php:
'openapi' => [
'info' => [
'title' => 'My Custom API',
'version' => '1.0.0',
],
'servers' => [
['url' => 'https://api.example.com/v1', 'description' => 'Production'],
],
],
scramble:extend command or service provider boot method:
Scramble::extend(function ($scramble) {
$scramble->addGlobalParameter('Authorization', 'header', 'string');
});
Integration with Laravel Features
ApiResource classes. Ensure your resources extend \Illuminate\Http\Resources\Json\JsonResource.FormRequest classes (Scramble infers schemas from validation rules):
public function rules(): array
{
return [
'email' => 'required|email',
'password' => 'string|min:8',
];
}
Route::get('/users/{user}', [UserController::class, 'show'])->name('users.show');
Testing API Documentation
public function test_api_docs_include_user_endpoint()
{
$spec = json_decode(file_get_contents(storage_path('app/scramble/api.json')), true);
$this->assertArrayHasKey('paths', $spec);
$this->assertArrayHasKey('/users', $spec['paths']);
}
phpunit/phpunit assertions.Deployment Workflow
/docs/api via a static host (e.g., Netlify) for production.docs/ branch or artifact registry (e.g., GitHub Releases).auth:sanctum). Customize security schemes in scramble.php:
'components' => [
'securitySchemes' => [
'bearerAuth' => [
'type' => 'http',
'scheme' => 'bearer',
'bearerFormat' => 'JWT',
],
],
],
morphMap in scramble.php to resolve polymorphic types:
'morphMap' => [
'App\Models\User' => 'users',
'App\Models\Post' => 'posts',
],
App\Http\Controllers\Api\V1\*), configure the scan paths:
'scan' => [
'paths' => [
app_path('Http/Controllers/Api/V1'),
],
],
Environment Restrictions
viewApiDocs gate will break access.Gate::define('viewApiDocs', fn ($user) => $user->isAdmin());
Caching Issues
php artisan scramble:clear-cache
'cache' => false in scramble.php).Complex Validation Rules
rules => ['posts.*' => 'required']) may not render intuitively in OpenAPI.FormRequest rules or extend the schema manually:
Scramble::extend(function ($scramble) {
$scramble->addSchema('Post', [
'type' => 'object',
'properties' => [
'title' => ['type' => 'string'],
'content' => ['type' => 'string'],
],
]);
});
Polymorphic Routes
Route::apiResource('posts', PostController::class)->except(['create', 'edit'])).morphMap carefully.Middleware Conflicts
scramble:ignore middleware. Ensure critical docs routes aren’t ignored:
Route::middleware(['scramble:ignore'])->group(function () {
// Non-doc routes
});
OpenAPI 3.1.0 Quirks
'openapi' => [
'version' => '3.0.3',
],
storage/app/scramble/api.json for errors or missing endpoints.scramble.php:
'debug' => true,
php artisan scramble:generate --verbose
Custom Schema Providers
Dedoc\Scramble\Contracts\SchemaProvider to inject custom schemas:
class CustomSchemaProvider implements SchemaProvider
{
public function getSchemas(): array
{
return [
'CustomModel' => [
'type' => 'object',
'properties' => ['custom_field' => ['type' => 'string']],
],
];
}
}
scramble.php:
'schema_providers' => [
\App\Providers\CustomSchemaProvider::class,
],
Post-Processing Hooks
scramble:post-process event:
Scramble::listen('scramble.post-process', function ($spec) {
$spec['info']['description'] = 'Enhanced API Docs';
return $spec;
});
UI Customization
php artisan vendor:publish --provider="Dedoc\Scramble\ScrambleServiceProvider" --tag="scramble-assets"
resources/views/vendor/scramble/docs.blade.php).Dynamic Documentation
scramble:dynamic to inject runtime data (e.g., feature flags):
Scramble::extend(function ($scramble) {
$scramble->addDynamicSchema('FeatureFlags', fn () => [
'type' => 'object',
'properties' => [
'new_ui' => ['
How can I help you explore Laravel packages today?