Controller with Middleware and return response in the controller not working in Laravel 5.1

15,678

Solution 1

You're not returning anything in handle() if $token isn't null. Try this:

public function handle($request, Closure $next)
{

    header('Access-Control-Allow-Origin: *');
    ...

    $token = $request->header('Token');

    if($token == null) return response('Not valid token provider.', 401);

    // return the $next closure, so other Middlewares can run
    return $next($request);
}

Solution 2

You forgot to return the response. just add return as below.

return $next($request);

Hope this help.

Share:
15,678
chemitaxis
Author by

chemitaxis

Updated on June 28, 2022

Comments

  • chemitaxis
    chemitaxis almost 2 years

    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).

    Routes:

    Route::group(['prefix' => 'api/v1', 'middleware' => 'token.api'], function () {
    
        Route::post('game/add/{id}', 'GameController@addGameToUser');    
    });
    

    The middleware is defined in the kernel.php correctly:

    protected $routeMiddleware = [
            'auth' => \App\Http\Middleware\Authenticate::class,
            'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
            'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
            'token.api' => \App\Http\Middleware\TokenMiddleware::class,
        ];
    

    I have removed

    \App\Http\Middleware\VerifyCsrfToken::class

    from the middlewares globals, because I just use AJAX API calls. ,

    In my middleware, I check just I have a Token header param:

    Middleware Code:

    <?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->header('Token');
    
            if($token == null)
                return response('Not valid token provider.', 401);
    
            else
            {
                $next($request);
            }
    
    
    
        }
    }
    

    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). If I remove the middleware from the controller, all works fine... I have no idea why...

    Controller Code:

    <?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()->json(['status'=>'ok','data'=>'data_example'], 200);
        }
    
    }
    

    Thank you so much!!