【问题标题】:Protecting Routes in Laravel 5.3在 Laravel 5.3 中保护路由
【发布时间】:2017-02-11 15:38:54
【问题描述】:

我正在尝试保护我在 Laravel 5.3 中的路线。我正在使用以下代码

Route::get('profile', function () {
    // Only authenticated users may enter...
})->middleware('auth');

如果我尝试在注销情况下浏览/profile,它会将我重定向到/login 路由。但我想将其重定向到/ 路由。

我该怎么做?

【问题讨论】:

    标签: php authentication routes laravel-5.3


    【解决方案1】:

    在 laravel 5.3 上,它位于 Exceptions 目录中。转到 App\Exceptions\Handler.php 并在底部更改代码:

        return redirect()->guest('/');
    

    【讨论】:

      【解决方案2】:

      你可以试试

      Route::group(['middleware'=>'web'],function (){
      Route::Auth();
      Route::get('/home', 'HomeController@index');});
      

      并更改 app\Middleware\RedirectIfAuthenticated.php

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

      【讨论】:

        【解决方案3】:
        public function handle($request, Closure $next, $guard = null)
        {
            if (Auth::guard($guard)->check()) {
                return redirect('/');
            }
        
            return $next($request);
        }
        

        请将此函数写入此文件app\Middleware\RedirectIfAuthenticated.php

        【讨论】:

        • 想要你做一个新的中间件??
        • 不,我没有制作任何中间件。谢谢
        【解决方案4】:

        更改文件app\Middleware\RedirectIfAuthenticated.php

        并编辑这一行:

        return redirect('/login');
        

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-24
        • 2016-02-11
        • 2019-09-13
        • 2023-03-28
        • 2017-05-15
        • 2017-05-25
        • 2017-08-10
        • 2015-03-11
        相关资源
        最近更新 更多