Laravel 登录后重定向回原来的目的地 [英] Laravel redirect back to original destination after login

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

问题描述

这似乎是一个非常基本的流程,Laravel 为基本的事情提供了很多不错的解决方案,我觉得我错过了一些东西.

This seems like a pretty basic flow, and Laravel has so many nice solutions for basic things, I feel like I'm missing something.

用户单击需要身份验证的链接.Laravel 的 auth 过滤器启动并将它们路由到登录页面.用户登录,然后转到他们在身份验证"过滤器启动之前尝试访问的原始页面.

A user clicks a link that requires authentication. Laravel's auth filter kicks in and routes them to a login page. User logs in, then goes to the original page they were trying to get to before the 'auth' filter kicked in.

有什么好方法可以知道他们最初尝试访问的页面是什么?由于 Laravel 是拦截请求的那个,我不知道它是否会在用户登录后跟踪某个地方以便于路由.

Is there a good way to know what page they were trying to get to originally? Since Laravel is the one intercepting the request, I didn't know if it keeps track somewhere for easy routing after the user logs in.

如果没有,我很想知道你们中的一些人是如何手动实现的.

If not, I'd be curious to hear how some of you have implemented this manually.

推荐答案

适用于 Laravel 5.3 及以上版本

在下面查看Scott 的回答.

简单地说,

关于身份验证中间件:

// redirect the user to "/login"
// and stores the url being accessed on session
if (Auth::guest()) {
    return redirect()->guest('login');
}
return $next($request);

登录操作:

// redirect the user back to the intended page
// or defaultpage if there isn't one
if (Auth::attempt(['email' => $email, 'password' => $password])) {
    return redirect()->intended('defaultpage');
}

对于 Laravel 4(旧答案)

在给出这个答案时,框架本身没有官方支持.现在你可以使用下面bgdrl指出的方法这个方法:(我试过更新他的答案,但他好像不接受)

在身份验证过滤器上:

// redirect the user to "/login"
// and stores the url being accessed on session
Route::filter('auth', function() {
    if (Auth::guest()) {
        return Redirect::guest('login');
    }
});

登录操作:

// redirect the user back to the intended page
// or defaultpage if there isn't one
if (Auth::attempt(['email' => $email, 'password' => $password])) {
    return Redirect::intended('defaultpage');
}

对于 Laravel 3(甚至更早的答案)

你可以这样实现它:

For Laravel 3 (even older answer)

You could implement it like this:

Route::filter('auth', function() {
    // If there's no user authenticated session
    if (Auth::guest()) {
        // Stores current url on session and redirect to login page
        Session::put('redirect', URL::full());
        return Redirect::to('/login');
    }
    if ($redirect = Session::get('redirect')) {
        Session::forget('redirect');
        return Redirect::to($redirect);
    }
});

// on controller
public function get_login()
{
    $this->layout->nest('content', 'auth.login'); 
}

public function post_login()
{
    $credentials = [
        'username' => Input::get('email'),
        'password' => Input::get('password')
    ];

    if (Auth::attempt($credentials)) {
        return Redirect::to('logged_in_homepage_here');
    }

    return Redirect::to('login')->with_input();
}

将重定向存储在 Session 上的好处是即使用户错过了输入他的凭据或者他没有帐户并且必须注册,也可以将其持久化.

Storing the redirection on Session has the benefit of persisting it even if the user miss typed his credentials or he doesn't have an account and has to signup.

这也允许除 Auth 之外的任何其他东西在会话上设置重定向,它会神奇地工作.

This also allows for anything else besides Auth to set a redirect on session and it will work magically.

这篇关于Laravel 登录后重定向回原来的目的地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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