2018-03-22 16:18
浏览 82

中间件优先级不适用于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';
      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 = [

protected $middlewarePriority = [


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dougui1977 2018-03-22 17:03

    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' => [

    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.


    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.

    点赞 打赏 评论

相关推荐 更多相似问题