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

查看:133
本文介绍了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及更高版本

在下面检查斯科特的答案.

简单地说,

在身份验证中间件上:

// 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天全站免登陆