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

查看:31
本文介绍了登录后 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天全站免登陆