我使用以下命令創(chuàng)建了一個(gè)自定義中間件php artisan make:middleware RedirectIfPasswordNotUpdated這是我的中間件<?phpnamespace App\Http\Middleware;use Closure;use Carbon\Carbon;use Illuminate\Support\Facades\Auth;use App;class RedirectIfPasswordNotUpdated{ public function handle($request, Closure $next) { if (!App::environment(['production'])) { return $next($request); } $user = Auth::user(); if (!$user->password_updated_at) { return redirect()->route('profile.password.edit')->with([ 'message' => 'Please update your password to proceed', 'alertType' => 'warning', ]); } if (Carbon::now()->diffInDays(Carbon::parse($user->password_updated_at)) > 90) { return redirect()->route('profile.password.edit')->with([ 'message' => 'Your password has expired! Please update your password to proceed', 'alertType' => 'warning', ]); } return $next($request); }}我想在我的控制器的構(gòu)造函數(shù)中使用這個(gè)中間件,如下所示public function __construct(){ $this->middleware('auth'); $this->middleware('RedirectIfPasswordNotUpdated');}當(dāng),我這樣做時(shí),我得到一個(gè)ReflectionException (-1)說Class RedirectIfPasswordNotUpdated does not exist我在其他 Laravel(v5.4、v5.6)項(xiàng)目中以相同的方式使用這個(gè)中間件,并且沒有任何問題。但它在當(dāng)前版本 (v5.8) 中不起作用。我究竟做錯(cuò)了什么?
1 回答

搖曳的薔薇
TA貢獻(xiàn)1793條經(jīng)驗(yàn) 獲得超6個(gè)贊
正如我所看到的,您尚未注冊(cè)您middleware class的app\Http\Kernel.php. 注冊(cè)一個(gè)中間件非常簡單,如下所示:
protected $routeMiddleware = [
'middle_name' => \App\Http\Middleware\RedirectIfPasswordNotUpdated::class,
]
- 1 回答
- 0 關(guān)注
- 131 瀏覽
添加回答
舉報(bào)
0/150
提交
取消