Laravel 5-登录后重定向到上一页 [英] Laravel 5 - After login redirect back to previous page

查看:100
本文介绍了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-登录后重定向到上一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆