laravel 类App\Http\UserMiddleware不存在

ncgqoxb0  于 2023-04-22  发布在  其他
关注(0)|答案(3)|浏览(161)

类App\Http\UserMiddleware不存在

<?php

namespace App\Http\Middleware;


use Closure;
Use Auth;

class UserMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */

 public function handle($request, Closure $next)

 {

     if(auth::check() && Auth::user()->role->id == 2){

         return $next($request);

     }
  else {

         return redirect()->route('login');

     }
 }
}
tag5nh1u

tag5nh1u1#

如果文件保存在App\Http而不是App\Http\Middleware中,则名称空间声明是错误的,并将引发异常

bvhaajcl

bvhaajcl2#

用这个

use App\Http\Middleware\UserMiddleware;
r9f1avp5

r9f1avp53#

将内核文件中的中间件更改为如下所示

protected $routeMiddleware = [
    'name_of_your_middleware' => 'App\Http\Middleware\UserMiddleware',
];

相关问题