title: How do you add headers to a response with a middleware?
body: <p>I can't figure out how to how to add headers to a response from a middleware. I've used both <code>-&gt;header(...)</code> and <code>-&gt;headers-&gt;set(...)</code> but both gives errors. So how do you do it?</p>
  
  <p>First I tried with</p>
  
  <pre><code>public function handle($request, Closure $next) {
      $response = $next($request);
  
      $response-&gt;headers-&gt;set('refresh', '5;url=' . route('foo'));
  
      return $response;
  }
  </code></pre>
  
  <p>which is the same as in <code>Illuminate\Http\Middleware\FrameGuard.php</code>, but that gives </p>
  
  <blockquote>
    <p>Call to a member function set() on a non-object</p>
  </blockquote>
  
  <p>Second I tried with </p>
  
  <pre><code>public function handle($request, Closure $next) {
      $response = $next($request);
  
      $response-&gt;header('refresh', '5;url=' . route('foo'));
  
      return $response;
  }
  </code></pre>
  
  <p>But that gives </p>
  
  <blockquote>
    <p>Method [header] does not exist on view.</p>
  </blockquote>
  
  <p>So how do you add headers from a middleware?</p>