How do I attach middlewear to a response builder?

Since Aero does not expose the underlying routes and controllers, middleware can be attached directly to the response builder. This allows for code to be executed before the main pipeline is run.

For example, to restrict all products from being accessible to guest visitors, you could register the auth middleware to the ProductPage response:


When assigning middleware, you can also pass the fully qualified class name:


Alternatively, you can provide a Closure. For example, middleware could be added to the homepage to set a tracking cookie from the referrer:

\Aero\Store\Http\Responses\Homepage::middleware(function ($request, $next) {
    return tap($next($request), function ($response) use ($request) {
        $response->cookie('referer', $request->header('Referer'));

Refer to the Laravel documentation for more information on middleware.

Articles in this section

Was this article helpful?
0 out of 0 found this helpful