Laravel Magazine
5 Laravel Middleware Tricks You Probably Are Not Using
Featured

5 Laravel Middleware Tricks You Probably Are Not Using

Most Laravel developers use middleware for authentication and maybe a CORS header or two, and then leave the rest of the feature set untouched. That is leaving a lot on the table. Here are five middleware patterns worth knowing. 1. Terminable Middleware — Run Code After the Response Is Sent Normal middleware runs before or after the controller, but still within the request/response cycle. Terminable middleware runs after the response has already been sent to the browser. This is ideal for expensive operations that the user should not have to wait for — logging, analytics, session writes, and cache warming. class RecordPageView implements TerminableMiddleware { public function handle(Request $request, Closure $next): Response { return $next($request); } public function terminate(Request $request, Response $response): void { // Runs after the response is sent — user never waits for this PageView::record( url: $request->fullUrl(), statusCode: $response->getStatusCode(), userId: $request->...

Recent Articles

Latest Links

Stay Updated

Subscribe to our newsletter

Get latest news, tutorials, community articles and podcast episodes delivered to your inbox.

Weekly articles
We send a new issue of the newsletter every week on Friday.
No spam
We'll never share your email address and you can opt out at any time.