沃梦达 / 编程问答 / php问题 / 正文

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

Laravel 5 - After login redirect back to previous page(Laravel 5 - 登录后重定向回上一页)

本文介绍了Laravel 5 - 登录后重定向回上一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

laravel 5.3 的解决方案:

Solution for laravel 5.3:

在 loginController 中将 showLoginForm() 函数覆盖如下:

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

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

它将设置url.intended"会话变量,该变量是laravel用于查找登录后要重定向的页面的变量,使用前一个url.

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.

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.

这篇关于Laravel 5 - 登录后重定向回上一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Laravel 5 - 登录后重定向回上一页

基础教程推荐