我使用以下命令創建了一個自定義中間件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); }}我想在我的控制器的構造函數中使用這個中間件,如下所示public function __construct(){ $this->middleware('auth'); $this->middleware('RedirectIfPasswordNotUpdated');}當,我這樣做時,我得到一個ReflectionException (-1)說Class RedirectIfPasswordNotUpdated does not exist我在其他 Laravel(v5.4、v5.6)項目中以相同的方式使用這個中間件,并且沒有任何問題。但它在當前版本 (v5.8) 中不起作用。我究竟做錯了什么?
1 回答

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