Installation
composer require dingo/blueprint
Add the service provider to config/app.php:
'providers' => [
// ...
Dingo\Blueprint\BlueprintServiceProvider::class,
],
Basic Setup
Register the blueprint in your routes/api.php:
$api = app('Dingo\Api\Routing\Router');
$api->version('v1', function ($api) {
$api->blueprint('api-blueprint.apib');
});
First Use Case Define a simple route and let the package auto-generate API Blueprint:
$api->get('users', 'App\Http\Controllers\UserController@index');
Run the generator:
php artisan blueprint:generate
Check storage/api-blueprint.apib for the output.
Route-Based Documentation
@api tags for metadata:
/**
* @api {get} /users Retrieve all users
* @apiName GetUsers
* @apiGroup Users
*/
public function index() { ... }
blueprint:generate to sync routes with the .apib file.Dynamic Blueprint Generation
$api->get('users/{id}', 'UserController@show')
->where('id', '[0-9]+');
The package auto-infers parameter types (e.g., id as integer).Customizing Output
php artisan vendor:publish --tag=blueprint-views
BlueprintGenerator class to modify rendering logic.API Versioning
$api->version('v1', function ($api) {
$api->blueprint('v1.apib');
});
$api->version('v2', function ($api) {
$api->blueprint('v2.apib');
});
@apiParam {String} query for clarity.@apiParam {String} [name] to mirror Laravel’s FormRequest rules.test('GET /users generates correct blueprint')
->getJson('/api/v1/users')
->assertOk();
Route Caching Conflicts
php artisan route:clear
php artisan blueprint:generate
.apib despite recent changes.Middleware Misrepresentation
/**
* @api {get} /admin/users Requires admin role
* @apiHeader {String} Authorization Bearer token
*/
Nested Resources
/users/{user}/posts/{post}—use explicit namespacing:
$api->get('users/{user}/posts/{post}', 'PostController@show')
->name('users.posts.show');
Verbose Output: Enable debug mode in config/blueprint.php:
'debug' => env('BLUEPRINT_DEBUG', false),
Logs generated blueprint steps to storage/logs/blueprint.log.
Partial Generates: Use --force to overwrite partial files:
php artisan blueprint:generate --force
Custom Attributes
Override BlueprintGenerator to add custom annotations:
// app/Extensions/CustomBlueprintGenerator.php
class CustomBlueprintGenerator extends BlueprintGenerator {
protected function parseAnnotations($method) {
// Add logic for @apiExample tags
}
}
Register in BlueprintServiceProvider:
$this->app->bind('blueprint.generator', function () {
return new CustomBlueprintGenerator();
});
Post-Processing
Hook into blueprint.generated event to modify the .apib file:
// In a service provider
event(new BlueprintGenerated($blueprintPath));
Non-Route Resources
Document non-route assets (e.g., WebSocket events) by extending the Blueprint class:
$blueprint->addResource('WebSocket', [
'events' => [
'chat.message' => ['description' => 'Broadcast a chat message']
]
]);
How can I help you explore Laravel packages today?