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

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.

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.

推荐答案

For Laravel 5.3 and above

勾选下面的Scott's answer.

对于Laravel 5至5.2

简单地说,

On auth middleware:

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

On login action:

// 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');
}

For Laravel 4 (old answer)

在回答这一问题时,框架本身没有得到官方支持.现在您可以使用下面bgdrl指出的方法:(I've tried updating his answer, but it seems he won't accept)

在身份验证筛选器上:

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

On login action:

// 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(更老的答案)

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();
}

将重定向存储在会话中有一个好处,即即使用户未键入其凭据,或者他没有帐户,必须注册,也可以保持重定向.

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

Laravel相关问答推荐

如何使中继器项目计数动态Laravel Filament V3?

如何使用PHP版本8.2.6安装beyondcode/laravel-websockets软件包?

工厂多对多数据透视表属性

如何使用 laravel 更新单个值

单个对象的Eloquent 计数是错误的

如何通过单击进入 vs 代码中的类

Laravel:子文件夹中的 lang 文件

Laravel 有Many Many to Many To One Eloquent

使用 Laravel 限制多态多对多关系中的相关记录

如何使用 Laravel 模型访问数据库视图?

如何创建具有关系的 Eloquent 模型?

Laravel 控制器构造

如何在本地 Laravel Homestead 站点上获取 https 证书

在 php Laravel 5 中创建自定义帮助函数的最佳实践是什么?

扩展模型 == 扩展 Eloquent?

Laravel 5 Mime 验证

如何重定向到laravel上的公共文件夹

基于日期的 Laravel 日志(log)文件

Laravel 集合计数结果

如何从 PHPUnit 测试设置运行 Laravel 数据库 seeder 机?