Official Laravel source
AttributeIlluminate\Routing\Attributes\Controllers\Middleware

#[Middleware]

Applies middleware to the Laravel target element, executing processing logic before the route.

Available since

Laravel 13.0

Targets

classmethod

Parameters

  • $middlewareClosure|string

    Middleware to be applied.

  • $only?array

    Apply middleware only to certain routes.

  • $except?array

    Exclude middleware application to certain routes.

Practical usage

use Illuminate\Routing\Attributes\Controllers\Middleware;

class UsersController {
    #[Middleware('auth', ['admin:read'], ['admin:write'])]
    public function index() {
        return ['data' => []];
    }
}