title: Controller with Middleware and return response in the controller not working in Laravel 5.1
body: <p>I am programming a Laravel 5.1 RestFUL API, but I have a very strange problem with the middleware and the controller response (is empty always).</p>
  
  <blockquote>
    <p>Routes:</p>
  </blockquote>
  
  <pre><code>Route::group(['prefix' =&gt; 'api/v1', 'middleware' =&gt; 'token.api'], function () {
  
      Route::post('game/add/{id}', 'GameController@addGameToUser');    
  });
  </code></pre>
  
  <p>The middleware is defined in the kernel.php correctly:</p>
  
  <pre><code>protected $routeMiddleware = [
          'auth' =&gt; \App\Http\Middleware\Authenticate::class,
          'auth.basic' =&gt; \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
          'guest' =&gt; \App\Http\Middleware\RedirectIfAuthenticated::class,
          'token.api' =&gt; \App\Http\Middleware\TokenMiddleware::class,
      ];
  </code></pre>
  
  <p>I have removed </p>
  
  <blockquote>
    <p>\App\Http\Middleware\VerifyCsrfToken::class</p>
  </blockquote>
  
  <p>from the middlewares globals, because I just use AJAX API calls.
  ,</p>
  
  <p>In my middleware, I check just I have a Token header param:</p>
  
  <blockquote>
    <p>Middleware Code:</p>
  </blockquote>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Middleware;
  use Closure;
  
  class TokenMiddleware
  {
      /**
       * Handle an incoming request.
       *
       * @param  \Illuminate\Http\Request  $request
       * @param  \Closure  $next
       * @return mixed
       */
      public function handle($request, Closure $next)
      {
  
          header('Access-Control-Allow-Origin: *');
          header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
          header('Access-Control-Allow-Headers: Origin, Content-Type, Token, Accept, Authorization, X-Request-With');
          header('Access-Control-Allow-Credentials: true');
  
  
          $token = $request-&gt;header('Token');
  
          if($token == null)
              return response('Not valid token provider.', 401);
  
          else
          {
              $next($request);
          }
  
  
  
      }
  }
  </code></pre>
  
  <p>In my controller (GameController), and in the method addGameToUser, I just return a JSON Test value, but the response is always empty (testing with postman). <strong>If I remove the middleware from the controller, all works fine..</strong>. I have no idea why...</p>
  
  <blockquote>
    <p>Controller Code:</p>
  </blockquote>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use Illuminate\Http\Request;
  
  
  
  use App\Http\Controllers\Controller;
  use App\UserGame;
  
  class GameController extends Controller
  {
      public function addGameToUser(Request $request, $idGame)
      {
          return response()-&gt;json(['status'=&gt;'ok','data'=&gt;'data_example'], 200);
      }
  
  }
  </code></pre>
  
  <p>Thank you so much!!</p>