php Laravel 5 - 登录后重定向回上一页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29954791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:40:43  来源:igfitidea点击:

Laravel 5 - After login redirect back to previous page

phplaravel-5

提问by cs1h

I have a page with a some content on it and a comments section. Comments can only be left by users who are signed in so I have added a login form to the page for users to sign in with (this only shows if they are not already logged in).

我有一个页面,上面有一些内容和一个评论部分。评论只能由已登录的用户留下,因此我在页面中添加了一个登录表单供用户登录(这仅在他们尚未登录时显示)。

The problem I have is that when the user signs in they get redirected back to the home page and not the page they were previously on.

我遇到的问题是,当用户登录时,他们会被重定向回主页,而不是他们之前所在的页面。

I have not changed the login method from the out of the box set-up.

我没有更改开箱即用设置的登录方法。

Can anyone suggest a simple way to set the redirect url. My thoughts are that it would be good to be able to set it in the form.

任何人都可以建议一种设置重定向网址的简单方法。我的想法是能够在表单中设置它会很好。

回答by Diguin

Solution for laravel 5.3:

laravel 5.3 的解决方案:

In loginController overwrite the showLoginForm() function as this one:

在 loginController 中,将 showLoginForm() 函数改写为:

public function showLoginForm()
{
    if(!session()->has('url.intended'))
    {
        session(['url.intended' => url()->previous()]);
    }
    return view('auth.login');    
}

It will set the "url.intended" session variable, that is the one that laravel uses to look for the page which you want to be redirected after the login, with the previous url.

它将设置“url.intended”会话变量,这是 Laravel 用来查找登录后要重定向的页面的变量,以及先前的 url。

It also checks if the variable has been set, in order to avoid the variable to be set with the login url if the user submit the form with an error.

它还检查是否已设置该变量,以避免在用户提交表单时出现错误时将变量设置为登录 url。

回答by Hasan AbdulRahim

For Laravel 5.5, following code worked for me by just updating LoginController.php

对于 Laravel 5.5,以下代码只需更新 LoginController.php 即可为我工作

public function showLoginForm()
{
    session(['link' => url()->previous()]);
    return view('auth.login');
}


protected function authenticated(Request $request, $user)
{
    return redirect(session('link'));
}

回答by bnqtoan

Please use redirect()->intended()instead in Laravel 5.1

redirect()->intended()在 Laravel 5.1 中使用

You can also see more about it here: http://laravel.com/docs/5.1/authentication

您还可以在此处查看更多相关信息:http: //laravel.com/docs/5.1/authentication

回答by Mahmoud Abdelhamid

For Laravel 5.3

对于 Laravel 5.3

inside App/Http/Controllers/Auth/LoginControlleradd this line to the __construct()function

在里面App/Http/Controllers/Auth/LoginController添加这一行到__construct()函数

$this->redirectTo = url()->previous();

So the full code will be

所以完整的代码将是

public function __construct()
{
    $this->redirectTo = url()->previous();
    $this->middleware('guest', ['except' => 'logout']);
}

It works like a charm for me i'm using laravel 5.3.30

它对我来说就像一个魅力我正在使用 laravel 5.3.30

回答by ImZedi

The Laravel 5.6, When user insert wrong credentials then login page will reload and session(['link' => url()->previous()]); will take login URL in link variable. So the user will redirect to a login page again or redirect to /home if login success. So to avoid these below code working for me! After that no matter how much time user insert wrong credentials he will redirect after login to exactly where he was before login page.

Laravel 5.6,当用户插入错误的凭据时,登录页面将重新加载和 session(['link' => url()->previous()]); 将在链接变量中获取登录 URL。因此,如果登录成功,用户将再次重定向到登录页面或重定向到 /home。所以为了避免这些下面的代码对我有用!之后,无论用户插入错误凭据多少时间,他都会在登录后重定向到登录页面之前的位置。

Update or overwrite public function showLoginForm() in LoginController.

更新或覆盖 LoginController 中的公共函数 showLoginForm()。

public function showLoginForm()
{
    if (session('link')) {
        $myPath     = session('link');
        $loginPath  = url('/login');
        $previous   = url()->previous();

        if ($previous = $loginPath) {
            session(['link' => $myPath]);
        }
        else{
            session(['link' => $previous]);
        }
    }
    else{
         session(['link' => url()->previous()]);
    }

    return view('auth.login');
}

Also, Update or Overwrite protected function authenticated(Request $request, $user) in LoginController.

此外,更新或覆盖在 LoginController 中验证的受保护函数(Request $request, $user)。

protected function authenticated(Request $request, $user)
{     
    return redirect(session('link'));      
}

回答by VipinKundal

For Laravel 5.4, following code worked for me by just updating LoginController.php

对于 Laravel 5.4,以下代码只需更新 LoginController.php 即可为我工作

use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\URL;


public function __construct()
{
    $this->middleware('guest', ['except' => 'logout']);
    Session::put('backUrl', URL::previous());
}


public function redirectTo()
{
    return Session::get('backUrl') ? Session::get('backUrl') :   $this->redirectTo;
}

回答by Loko

Look into laravel cheat sheet

查看Laravel 备忘单

and use:

并使用:

URL::previous();

to go to the previous page.

转到上一页。

回答by George

Redirect to login with the current's page url as a query string:

使用当前页面 url 作为查询字符串重定向到登录:

<a href="{{ url('login?redirect_to=' . urlencode(request()->url())) }}">login</a>

In your LoginController check if exists and save the query string in session then redirect to the url after login

在您的 LoginController 检查是否存在并将查询字符串保存在会话中,然后在登录后重定向到 url

public function __construct() {
        parent::__construct();
        if ( \request()->get( 'redirect_to' ) ) {
            session()->put( 'redirect.url', \request()->get( 'redirect_to' ) );
        }
        $this->middleware( 'guest' )->except( 'logout' );
}

protected function authenticated(Request $request, $user) {
        if(session()->has('redirect.url') {
             return redirect( session()->get( 'redirect.url' ) );
        }
}

回答by Adam

If you want to redirect always to /homeexcept for those pages with comments, then you should overwrite your redirectTomethod in your LoginController:

如果您想始终重定向到/home带有注释的页面除外,那么您应该redirectTo在 LoginController 中覆盖您的方法:

public function redirectTo()
{
     return session('url.intended') ?? $this->redirectTo;
}

On all pages where you want to remain on the site, you should store the url for one request in the session:

在您希望保留在站点上的所有页面上,您应该在会话中存储一个请求的 url:

public function show(Category $category, Project $project){
   // ...

   session()->flash('url.intended' , '/' . request()->path());
}

回答by ???? ????? ?????? ???? ? ?????

in your RedirectIfAuthenticated.php change this code

在您的 RedirectIfAuthenticated.php 中更改此代码

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

        return $next($request);
    }

please notice to :

请注意:

return redirect()->intended('/contactus');