Routes & operations
Each route of a document becomes one operation per HTTP method. This page covers which routes are documented and the metadata of their operations: summary, description, tags, operation id and deprecation.
Choose the routes
The routes of a document are those matching routes.include and none of routes.exclude. By default, every route
under api/:
'documents' => [
'v1' => [
'routes' => [
'include' => ['api/*'],
'exclude' => ['api/internal/*'],
'ignored_methods' => ['HEAD'],
'order' => 'registration', // or 'path'
],
],
],
The masks are shell masks on the URI. To match on something else, give a map of masks by uri, name, domain and
middleware (a declared name or a class); a route matches when one mask matches it:
'routes' => [
'include' => ['uri' => ['api/*'], 'middleware' => ['auth:sanctum']],
'exclude' => ['name' => ['*.debug']],
],
An empty include (or null) keeps every route.
Some routes are never documented:
- the internal routes of Laravel and of common packages, listed in
routes.internal(up,sanctum/csrf-cookie,telescope/*,horizon/*,livewire/*…); remove an entry from the list to document it; - redirects (
Route::redirect()), views (Route::view()) and the fallback route; - a controller, an action or a closure marked
#[Exclude].
use Ark4ne\OpenApi\Attributes\Exclude;
final class UserController
{
#[Exclude]
public function debug(): array
{
// …
}
}
php artisan openapi:inspect <route> says whether it is in the document.Paths and path parameters
The URI of the route is the path. Its parameters are typed from what the route and the action declare:
| Declaration | Schema of the parameter |
|---|---|
int $user in the action | integer |
User $user, a bound model | the type of its route key (integer for id), and a 404 |
Status $status, a backed enum | a reference to the Status component, and a 404 |
->whereNumber('user'), ->whereUuid(), ->whereIn() | integer, string with format: uuid, an enum |
{post:slug}, a custom binding key | the type of slug; the key is removed from the path |
| Anything else | string |
An optional parameter at the end of the URI gives two paths: users/{user?} is documented as /api/users and
/api/users/{user}.
A domain (Route::domain('{account}.example.com')) gives the operation its own server,
https://{account}.example.com, with account as a server variable.
To describe a path parameter, use #[PathParameter] on the action:
use Ark4ne\OpenApi\Attributes\PathParameter;
#[PathParameter(
'catalog',
type: 'non-empty-string',
description: 'Catalog code',
example: 'fr',
)]
public function search(SearchRequest $request, string $catalog): array
Summary and description
The PHPDoc of the action gives them: the first line is the summary, the rest is the description (CommonMark).
/**
* List the users.
*
* Sorted by name. Only the **active** users are listed.
*/
public function index(): AnonymousResourceCollection
#[Operation] replaces them, on the action or on the controller (where it fills only what the action leaves empty):
use Ark4ne\OpenApi\Attributes\Operation;
#[Operation(
summary: 'Fetch a user',
description: 'Returns the profile of a user.',
operationId: 'getUser',
)]
public function show(User $user): UserResource
Without PHPDoc nor attribute, the summary comes from strategies.summary: doc (the default, none without PHPDoc),
name (the route name), uri or controller (UserController@show).
With languages configured, the summary, the description and the tags may be translation keys; see
Translations.
Tags
By default, an operation is tagged by its controller: strategies.tags with by: controller and a pattern whose
first group is the tag. With the published configuration, App\Http\Controllers\UserController is tagged User,
and App\Http\Controllers\Admin\UserController is tagged Admin\User. A controller the pattern does not match is
tagged by its class name without Controller.
strategies.tags.by | Tag of GET /api/v1/users/{user}, route api.users.show, UserController |
|---|---|
controller | User |
uri | users: the first segment after api and the version |
name | users: the first segment after api |
#[Tag] adds a tag and describes it; #[Operation(tags: [...])] replaces the tags of the strategy:
use Ark4ne\OpenApi\Attributes\Tag;
#[Tag('users', description: 'The accounts of the shop')]
final class UserController
Tag groups
A tag can have a parent: #[Tag('users', parent: 'admin')], or every tag at once with strategies.groups
(controller: the namespace under Controllers, Admin for App\Http\Controllers\Admin\UserController; uri;
name). The parent is written as Tag.parent in 3.2, and as x-tagGroups before 3.2 with
vendor_extensions: redoc. summary and kind of #[Tag] are 3.2 fields.
Operation id
strategies.operation_id names the operations. The published configuration uses method_name; without the key, the
default is name.
by | GET /api/users/{user}, route users.show, UserController@show |
|---|---|
method_name | GET:users.show |
name | users.show |
action | UserController.show |
method_path | getApiUsersByUser |
A route without name (or without controller for action) falls back to method_path. Operation ids are unique: a
second operation with the same id gets a _2 suffix and a warning. A route answering several methods
(Route::match(['put', 'patch'], …)) needs method_name or method_path to avoid it.
Deprecation
@deprecated in the PHPDoc, or PHP's native #[\Deprecated], marks the operation deprecated;
#[Operation(deprecated: false)] overrides both.
External documentation
#[Operation(
externalDocs: 'https://docs.example.com/users',
externalDocsDescription: 'The user guide',
)]
The document's own external documentation is external_docs in its configuration.