【问题标题】:Wrong Redirection in Laravel 5 built in User AuthenticationLaravel 5 中内置用户身份验证的错误重定向
【发布时间】:2015-07-14 10:59:28
【问题描述】:

我正在尝试使用 Laravel 5 用户身份验证。在这方面,我的 URL 是http://localhost/lara_project/public/auth/login 登录前。我想在成功登录后将用户重定向到http://localhost/lara_project/public/home。但它被重定向到http://localhost/lara_project/public/。我尝试了如下路线

Route::get('home',['middleware' => 'auth', 'uses' => 'WelcomeController@index']);

Route::get('/', 'HomeController@index');

Route::controllers(['auth' => 'Auth\AuthController','password' => 'Auth\PasswordController']);

位于 routes.php

我的问题是成功登录用户重定向到http://localhost/lara_project/public/,如果我想在成功登录后浏览http://localhost/lara_project/public/home,则会显示错误

This webpage has a redirect loop
ERR_TOO_MANY_REDIRECTS

我尝试编辑 RedirectIfAuthenticated.phpcompiled.phpAuthenticatesAndRegistersUsers 文件。但未能得到满意的结果。

有人可以在这方面帮助我吗?

【问题讨论】:

    标签: php authentication laravel-5


    【解决方案1】:

    转到 Auth\AuthController 并添加一个变量 $redirectTo = 'home';

    这告诉 laravel 登录后重定向到哪里。

    【讨论】:

    • 感谢@Carthagian Traveler 的回复。您的解决方案不起作用。谢谢
    【解决方案2】:

    修改RedirectIfAuthenticated.php如下

    public function handle($request, Closure $next)
    {
        if ($this->auth->check() && !$request->is('/home'))
        {
            return redirect('/home');
        }
    
        return $next($request);
    }
    

    也请看这里。我正在使用某种代码,如下所示。

    public function handle($request, Closure $next)
    {
        if (!$request->is('authenticate') && !$request->is('authenticate/login') && !Auth::check()) {
    
            return $request->ajax() ? response(array("status"=>'404')) : redirect('authenticate/login');
        }
        if(Auth::check()) {
            if( ! $request->user()->isAdmin() ) {
                return $request->ajax() ? response(array("status"=>'404')) : redirect('authenticate/login');
            }
            return $next($request);
        }
        return $next($request);
    }
    

    【讨论】:

    • 感谢@Pratik Soni。您的解决方案不起作用。谢谢
    • 你可以试试!$request->is('home') 告诉我吗?
    • 我试过!$request->is('home')。现在错误This webpage has a redirect loop ERR_TOO_MANY_REDIRECTS 没有出现,我可以看到home 页面。但成功登录后,它会重定向到http://localhost/lara_project/public/。我需要重定向到http://localhost/lara_project/public/home
    • 我已经编辑了代码。请查看代码并告诉我
    • 感谢@Pratik Soni。您的解决方案不起作用。谢谢
    【解决方案3】:

    转到/app/Http/Controllers/Auth/LoginController.php

    您可以在此处将protected $redirectTo = RouteServiceProvider::HOME; 更改为您想要的。

    【讨论】:

      猜你喜欢
      • 2016-04-02
      • 2019-04-25
      • 2015-11-11
      • 2016-07-25
      • 2022-01-24
      • 2016-01-25
      • 1970-01-01
      • 2020-01-07
      • 2015-08-21
      相关资源
      最近更新 更多