Attributes and middleware
Fusion Stack reads PHP attributes on controllers, resources, form requests, queries and error codes. An attribute on a controller class applies to each of its actions; one on an action wins over the class's.
| Attribute | Goes on | What it does |
|---|---|---|
#[Endpoint] | Action, controller or closure | Summary and description in the API reference. |
#[RequiresScope] | Action or controller | The scope a request needs. |
#[Describe] | Resource, form request or query | A field's description, example and allowed values. |
#[Description] | Error code enum case | What an error code means. |
#[CacheControl] | Action or controller | The Cache-Control header. |
#[CacheResponse] | Action or controller | Caches responses on the server. |
#[McpTool] | Action or closure | Exposes the endpoint as an MCP tool. |
#[McpResource] | Action or closure | Exposes the endpoint as an MCP resource. |
#[Endpoint]
FusionStack\Laravel\OpenApi\Endpoint describes an endpoint in the OpenAPI document and the API reference.
#[Endpoint('List orders', 'Orders for the authenticated team, newest first.')]
public function index(OrderQuery $query): AnonymousResourceCollection| Argument | Default | What it does |
|---|---|---|
summary | none | A short title, such as List orders. |
description | none | More detail, in Markdown. |
tags | The first path segment after the version prefix | Groups endpoints in the reference. |
resource | From the return type | The JsonApiResource class the action returns, when its return type does not say. |
status | 200, or 201 for a POST returning a resource | The success status. |
hidden | false | Leaves the endpoint out of the document. |
On a closure route, put it on the closure: Route::get('ping', #[Endpoint('Ping')] fn () => []). api:docs:lint reports endpoints without a summary.
#[RequiresScope]
FusionStack\Laravel\Auth\RequiresScope names a scope the request needs. Guests get 401 unauthenticated, and clients without the scope get 403 insufficient_scope, with the scope in the error's meta.required_scope. Repeat it to require several. An endpoint without one needs no credentials.
#[RequiresScope('orders:write', 'Place and change orders.')]
public function store(StoreOrderRequest $request): OrderResource| Argument | What it does |
|---|---|
scope | The scope, such as orders:write. A client with * has every scope. |
description | What the scope allows, for the API reference. Give it once per scope. |
For a route without a controller, use the scope middleware instead.
#[Describe]
FusionStack\Laravel\OpenApi\Describe describes one field. Repeat it on the class, once per field:
#[Describe('status', 'Where the order is in fulfilment.', values: [
'pending' => 'Placed, awaiting payment',
'paid' => 'Paid, not yet shipped',
])]
#[Describe('total', 'The total in the smallest unit of the currency.', example: 2500)]
final class OrderResource extends JsonApiResource| Argument | What it does |
|---|---|
field | The field, named as in the latest version. |
description | What the field means. |
example | An example value, used in request examples. For fields that are not columns, such as accessors, it also sets the type. |
values | What each allowed value means. It also limits the field to those values in the schema. |
What a field is depends on the class:
| On | A field is |
|---|---|
A JsonApiResource | An attribute or relationship. |
| A form request | A rule key, or the attribute or relationship name alone for JSON:API bodies: total for data.attributes.total. |
An ApiQuery | A filter. |
Resources and form requests can also return descriptions from a describe() method, whose entries win over the attributes. Each entry is a description, or an array with description, example and values:
public function describe(): array
{
return [
'currency' => ['description' => 'EUR, GBP or USD.', 'example' => 'GBP'],
'team' => 'The team placing the order.',
];
}#[Description]
FusionStack\Laravel\OpenApi\Description explains an error code in the API reference: when clients get it, and what to do. Cases without one are described by their title. See errors.
#[Description('Only paid and shipped orders have an invoice to send.')]
case OrderNotPaid = 'order_not_paid';#[CacheControl]
FusionStack\Laravel\Caching\CacheControl sets the Cache-Control header of successful GET responses, instead of api.http_cache.cache_control and any cache-control route middleware.
#[CacheControl(maxAge: 60, private: true)]
public function show(OrderQuery $query, string $order): OrderResource| Argument | Directive |
|---|---|
maxAge | max-age=… |
sMaxAge | s-maxage=…, for shared caches |
public | public. It cannot be combined with private. |
private | private |
noCache | no-cache |
noStore | no-store |
mustRevalidate | must-revalidate |
staleWhileRevalidate | stale-while-revalidate=… |
immutable | immutable |
See caching for when to use each.
#[CacheResponse]
FusionStack\Laravel\Caching\CacheResponse keeps successful GET responses on the server, in the latest version's shape, so one entry serves every pinned version. Responses carry X-Response-Cache: hit or miss.
#[CacheResponse(ttl: 60, tags: ['orders:{order}', 'teams'])]
public function show(OrderQuery $query, string $order): OrderResource| Argument | Default | What it does |
|---|---|---|
ttl | 60 | Seconds to keep an entry. |
tags | [] | Flushing any of these drops the entry. {order} is replaced by the route parameter, so orders:{order} becomes orders:42. |
shared | false | Shares entries between callers. Use it only for data every caller may see. |
A model using the FusionStack\Laravel\Caching\FlushesResponseCache trait flushes its table's tag, such as orders, and its own, such as orders:42, when it is saved or deleted. Override responseCacheTags() to flush more.
WARNING
A cache hit skips the controller, so checks inside it, such as policies or ApiQuery::scope(), do not run. Entries are kept per client unless shared is true.
#[McpTool]
FusionStack\Mcp\McpTool, from the optional fusion-stack/mcp package, exposes an endpoint as an MCP tool. Nothing is exposed without it. See the MCP guide.
| Argument | Default | What it does |
|---|---|---|
name | The endpoint's summary in snake case, such as list_orders | The tool's name. |
description | The endpoint's summary and description | What the tool does. |
destructive | none | Required for endpoints that are not GET: true for deleting or cancelling, false for only adding. |
#[McpResource]
FusionStack\Mcp\McpResource exposes a GET endpoint without path parameters as a read-only MCP resource, for reference data such as order statuses.
| Argument | Default |
|---|---|
uri | api:// and the endpoint's path, such as api://v1/order-statuses |
name | The endpoint's summary in snake case |
description | The endpoint's summary and description |
Middleware
The fusion-stack group
Routes in the fusion-stack middleware group get Fusion Stack's behaviour, and appear in the OpenAPI document:
Route::prefix('v1')->middleware('fusion-stack')->group(function (): void {
Route::apiResource('orders', OrderController::class);
});It runs, in this order, which the package fixes:
- Request ID: reads or creates
X-Request-Id. - Client: authenticates the request with the configured guards.
- Version: resolves the version, and sets
Api-Versionon the response. - Deprecation: sets
Deprecation,SunsetandLinkfor deprecated versions. - Route in version: 404 or 410 for routes the version does not have.
- Rate limit.
- Scopes: checks
#[RequiresScope]and thescopemiddleware. - Idempotency.
- Request transform: migrates older clients' bodies to the latest version.
- Response transform: migrates responses back to the client's version.
- HTTP cache:
ETag,304 Not Modified,Cache-ControlandVary. - Response cache:
#[CacheResponse]. - Legacy controllers: dispatches frozen actions for older versions.
Request IDs, authentication, rate limiting, versioning, idempotency, ETags and the response cache each have a switch in configuration.
scope
scope:orders:write requires a scope on routes without a controller to put #[RequiresScope] on. List several to require each:
Route::get('reports', fn () => ...)->middleware('scope:reports:read');cache-control
cache-control:public,max-age=60 sets the Cache-Control header for a route or group's successful GET responses. #[CacheControl] on the controller wins over it.