douliu3831 2018-03-22 16:18
浏览 86
已采纳

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

  • 写回答

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看