Search code examples
laravellaravel-5.3middleware

Laravel 5.3 middleware guard not working


Recently I've upgraded to Laravel 5.3 from Laravel 5.2 and after that I've found the Localization middleware that I've created for setting user's language has stopped working, not sure why?

I am using token based authentication.

The middleware is as:

<?php namespace App\Http\Middleware;

use Closure;
use PhpSpec\Exception\Exception;
use Illuminate\Support\Facades\Auth;


class Localization {

    public function handle($request, Closure $next, $guard = null)
    {
       if(!Auth::guard()->guest())
       {
            if(!session('current_locale'))  {
                $localecode = Auth::guard($guard)->user()->getLocale();
                session('current_locale',empty ($localecode) ? 'en' : $localecode);
                \App::setLocale($localecode);
            }else{
                \App::setLocale(session('current_locale'));
            }
       }
        return $next($request);
    }

}

And my routes/api.php is as:

Route::group(['prefix' => 'api/service/v1', 'middleware' => ['api'], 'guard' => 'api'], function(){
    Route::get("someUrlHere","SomeControler@SomeMethod");
    Route::group(['middleware' => ['auth:api','localization','beforeMiddleware', 'afterApiCallMiddleware'], 'guard' => 'api'], function() {
        Route::get("SomeOtherUrlHere","SomeOtherControler@SomeOtherMethod");
    });
});

The config/auth.php is as:

<?php

return [
    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
    ],

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],
    ],

    'passwords' => [
        'users' => [
            'provider' => 'users',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],
];

The app/Http/Kernal.php is as:

<?php namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel {

    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,  
        'Barryvdh\Cors\HandleCors',

    ];

    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];

    protected $routeMiddleware = [
        'auth' => 'App\Http\Middleware\Authenticate',
        'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',     
        'guest' => 'App\Http\Middleware\RedirectIfAuthenticated',
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'localization'=>'App\Http\Middleware\Localization',
        'afterMiddleware' => 'App\Http\Middleware\AfterMiddleware',
        'beforeMiddleware' => 'App\Http\Middleware\BeforeMiddleware',
        'afterApiCallMiddleware' => 'App\Http\Middleware\AfterApiCall',
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ];

}

Each time calling the method I am finding the $guard is null in handle() method of the Localization middleware.


Solution

  • Because $guard is an additional param in the middleware you will have to pass it through manually. You should be able to do this be changing the value in your Route::group middleware array from:

    'localization'
    

    to:

    'localization:api'
    

    Hope this helps!