中间件优先级不适用于Laravel 5.5

I'm stuck on something which I'm pretty sure is quite basic.. but somehow I can't get it to work.

I created a middleware to set the locale of my website depending on the user's choice (his choice is registered in the database under 'locale' column). What I do is that I check is user is logged-in, if he is I use his locale, if not I default to English.

For some reason, my middleware never "sees" the user as logged-in and always defaults to English. I've output the result of Auth::check() to a log and indeed this always returns false, even if the user is actually logged-in.

I suppose this has to do with this Middleware being executed before the Auth one.. I've tried to change the priority in the Kernel file but that still doesn't work.. any idea?

My middleware

namespace App\Http\Middleware;

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

class SetLocale {

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

      if(Auth::check()) {
        $locale = Auth::user()->locale;
      } else {
        $locale = 'en';
      }
      \App::setLocale($locale);
      return $next($request);
    }
}

My route ('auth' is the default auth middleware, 'checkProjectPermission:read' checks if the user logged in has permission to access this project)

Route::prefix('client')->middleware(['auth','checkRoomPermission:read'])->group(function() {
    Route::get('{project_id}/checklist', 'ProjectController@accessChecklist')->name('checklist');
}

My Kernel.php file :

protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
    \App\Http\Middleware\TrimStrings::class,
    \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    \App\Http\Middleware\TrustProxies::class,
    \App\Http\Middleware\SetLocale::class
];

protected $middlewarePriority = [
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    \Illuminate\Auth\Middleware\Authenticate::class,
    \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    \Illuminate\Session\Middleware\AuthenticateSession::class,
    \Illuminate\Routing\Middleware\SubstituteBindings::class,
    \Illuminate\Auth\Middleware\Authorize::class,
    \App\Http\Middleware\SetLocale::class
];

Thanks

The problem is that you're adding SetLocale to your global middleware. The global middleware does not start a session and therefore you have no access to session based data - such as the user.

If you take a look at the web middleware provided by laravel (cleaned up for brevity)

protected $middlewareGroups = [
    'web' => [
        EncryptCookies::class,
        AddQueuedCookiesToResponse::class,
        CreateFreshApiToken::class,
        StartSession::class,
        ShareErrorsFromSession::class,
        VerifyCsrfToken::class,
        SubstituteBindings::class
    ]
]

You'll see why the any middleware added to web middleware will have access to the auth class, because it injects StartSession.

If you want to access session-based-data, then you'll need to make sure StartSession is booted.

DO NOT just add StartSession to your global middleware or you're going to have a bad time. Instead, add it to your auth middleware, and add the SetLocale after StartSession.

Edit

Note that if you're using Web Middleware you must add SetLocale to that Middleware Group as well.

The priority is only a nsort where it reorders the middleware applied to a current request based on the existence of the middleware in the middlewarePriority array.

Simplified: If the middleware is not in the middleware group for the route request, the middleware priority means nothing.