How do you add headers to a response with a middleware?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# Mastering Response Manipulation: Adding Headers within Middleware As senior developers working with frameworks like Laravel, understanding how middleware interacts with the HTTP response lifecycle is crucial. When you step into the world of middleware, you are essentially intercepting the request/response flow. A common hurdle developers face is manipulating the headers of the response object received from the next layer in the stack. The errors you encountered—`Call to a member function set() on a non-object` and `Method [header] does not exist on view`—highlight a misunderstanding of which object you are attempting to modify. This post will dive deep into the correct, idiomatic way to add headers within a Laravel middleware, providing a robust solution for controlling response metadata. We will move beyond the failed attempts and establish a pattern that works reliably across the framework. ## Understanding the HTTP Response Flow In frameworks built on Symfony components (which Laravel heavily utilizes), the `$next($request)` call returns an object representing the final HTTP response. This object is not always a simple view or basic PHP object; it is usually an instance of `Illuminate\Http\Response` or its underlying Symfony counterpart, which handles the actual transmission details. The reason your initial attempts failed is that you were trying to call methods directly on whatever `$response` variable held, without accessing the specific interface that allows header modification during this phase of the request cycle. You need to interact with the response object in a way that respects the framework's internal structure. ## The Correct Approach: Using the Response Object To correctly add headers from within middleware, you must access the methods available on the actual HTTP response object being processed. In Laravel, this is typically done by ensuring your middleware receives and manipulates the `Response` object appropriately. The most reliable way to manipulate headers—especially for things like caching or redirection headers—is to ensure you are operating on the correct type of response object provided by the framework. If you are working within a standard Laravel context, access methods that deal directly with HTTP headers, rather than generic object properties. Here is how you correctly implement header manipulation in a middleware: ```php use Closure; use Illuminate\Http\Request; use Illuminate\Http\Response; // Import the necessary class if needed for strict typing class CustomHeaderMiddleware { public function handle(Request $request, Closure $next) { // 1. Pass the request down the chain to get the actual response $response = $next($request); // 2. Check if the response object supports header manipulation (e.g., Illuminate\Http\Response) if ($response instanceof Response) { // Use the correct method provided by the Response object for setting headers $response->headers->set('refresh', '5;url=' . route('foo')); } return $response; } } ``` ### Why This Works By explicitly checking if `$response` is an instance of `Illuminate\Http\Response` (or a related class), you ensure that you are calling methods that the framework expects for response manipulation. This avoids the errors you saw because you are no longer attempting to call non-existent methods on generic objects like `view` or raw response wrappers. This pattern is critical when building custom request pipelines, as demonstrated by patterns found in robust applications built with Laravel principles, such as those detailed on [laravelcompany.com](https://laravelcompany.com). ## Best Practices for Middleware Header Management When managing headers within middleware, keep these best practices in mind: 1. **Type Hinting:** Always leverage PHP's type hinting to ensure you are working with the correct object types, improving code safety and preventing runtime errors like those experienced previously. 2. **Response Object Focus:** Always focus your manipulation logic on the response object returned by `$next($request)`. If you need to modify headers before they are sent, this is the gateway. 3. **Separation of Concerns:** For complex header logic, consider abstracting it into dedicated Service Classes or Traits rather than cluttering your core middleware functions. This keeps your pipeline clean and maintainable. ## Conclusion Adding headers from a middleware requires understanding the specific object structure provided by the HTTP layer of your framework. The key takeaway is to stop treating `$response` as an arbitrary object and instead treat it as the concrete `Response` instance that Laravel provides. By correctly casting or checking the type, you unlock the proper methods for header manipulation, allowing you to seamlessly control response metadata throughout your application's request lifecycle. Mastering this flow is essential for building powerful and predictable APIs following modern architectural standards.