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 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?