【问题标题】:Midedlware class is not working in Laravel 5.3Midedlware 类在 Laravel 5.3 中不起作用
【发布时间】:2017-03-28 18:43:20
【问题描述】:

我在这个应用程序中有一个 laravel 应用程序,我有以下登录用户功能

     public function login() {
    try {
        $inputs = Input::except('_token');
        $validator = Validator::make($inputs, User::$login);
        if ($validator->fails()) {
            return Redirect::to('/')->with('message', 'Please Enter Valid Credentials');
        } else {
            $respones = \UserHelper::processLogin($inputs);
            if ($respones) {

                return Redirect::to('/dashboard')->with('success_message', 'Welcome to Tressly Admin Dashboard');
            } else {
                return Redirect::to('/')->with('message', 'Please Enter Valid Information ');
            }
        }
    } catch (Exception $ex) {
        return CommonHelper::AdminExceptions($ex);
    }
}

现在,当用户注销并按下后退按钮时,浏览器会显示上一页,因为它存在于缓存中。现在在此页面上,因为用户尝试访问任何受保护的路由应用程序它显示以下错误 我想将它重定向到'/'(主路由),因为注销的用户试图访问任何保护路由,错误来了

   Class App\Illuminate\Auth\Middleware\AdminAuthenticate does not exist

我做了一个自定义的Authentication Middle,中间件的handle函数是

      public function handle($request, Closure $next, $guard = null) {
    if (Auth::check()) {
        return $next($request);
    }
    return redirect('/');
}

我也在 $routeMiddleware 中的 kernal.php 中注册了它

   'authAdmin' => \Illuminate\Auth\Middleware\AdminAuthenticate::class,

并像

一样保护我的路线
  Route::group(['middleware' => 'authAdmin'], function () {
      ///routes 
   });

有什么想法吗?

【问题讨论】:

    标签: php laravel redirect


    【解决方案1】:

    使用

        'authAdmin' => \App\Http\Middleware\AdminAuthenticate::class,
    

    代替

       'authAdmin' =>\Illuminate\Auth\Middleware\AdminAuthenticate::class,
    

    我希望它会起作用

    【讨论】:

    • 类身份验证不起作用。我在 AdminAuthenticate 中使用了 Auth,谢谢伙计,它现在正在工作。你拯救了我的一天。
    【解决方案2】:

    您是否有理由创建一个自定义中间件类,它与已经存在的“auth”中间件完全相同?

    RedirectifAuthenticated.php 会这样做;

    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            return redirect('/home');
        }
    
        return $next($request);
    }
    

    https://laravel.com/docs/5.3/authentication#protecting-routes

    【讨论】:

      猜你喜欢
      • 2017-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-14
      • 2017-02-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多