title: Laravel Middleware : Call to a member function header() on array
body: <p>In my project there are two levels of middleware.</p>
  
  <p>One UserAuthentication, and another is PermissionsMiddleWare.</p>
  
  <p>Suppose there is a route  : </p>
  
  <pre><code>Route::group(['middleware' =&gt; ['myauth', 'revalidate'], 'prefix' =&gt; '/user'], function () {
      Route::group(['middleware' =&gt; 'permissions:Campaigns'], function () {
        Route::resource('/dashboard', 'UserController@dashboard');
      }
  }
  </code></pre>
  
  <p>Now in UserAuthenticationMiddleware:</p>
  
  <pre><code>     &lt;?php
  public function handle($request, Closure $next)
  {
      if ($request-&gt;session()-&gt;has('user_id')) {
          $user_id = $request-&gt;session()-&gt;get('user_id');
      } else {
          return redirect('loginUser');
      }
  
      $response = $next($request);
      return $response-&gt;header('Cache-Control', 'nocache, no-store, max-age=0, must-revalidate')
              -&gt;header('Pragma', 'no-cache')
              -&gt;header('Expires', 'Fri, 01 Jan 1990 00:00:00 GMT');
  
      return $next($request);
  }
  </code></pre>
  
  <p>And in PermissionsMiddleware:</p>
  
  <pre><code>&lt;?php
  public function handle(Request $request, Closure $next, $permission_name = "")
  {
  //login to get permission decision
      if (!$decision) {
  // **Old process** of response
  // return redirect('user/accessRejected')-&gt;with('message', 'Request rejected');
  // **New process** of response
          return ['accessRejected' =&gt; true, 'message' =&gt; 'Request rejected'];
      }
  
  
      $response = $next($request);
      return $response-&gt;header('Cache-Control', 'nocache, no-store, max-age=0, must-revalidate')
              -&gt;header('Pragma', 'no-cache')
              -&gt;header('Expires', 'Fri, 01 Jan 1990 00:00:00 GMT');
  
      return $next($request);
  }
  </code></pre>
  
  <p><strong>Old process</strong> is working fine, and permissions middleware is restricting properly and redirecting to the page.</p>
  
  <p>Now, as this is API side of project , so I cannot redirect to another page rather I need to response in JSON or array format. </p>
  
  <p><strong>When following the new process of response I am getting this error:</strong></p>
  
  <pre><code>FatalErrorException in UserAuthenticationMiddleware.php line (this below code line):
  -&gt; return $response-&gt;header('Cache-Control','nocache, no-store, max-age=0, must-revalidate')
              -&gt;header('Pragma','no-cache')
              -&gt;header('Expires','Fri, 01 Jan 1990 00:00:00 GMT');
  Call to a member function header() on array
  </code></pre>
  
  <p>Please give some light on this to know what is wrong in my code.</p>