商店&a​​mp;登录Laravel后检索会话

时间:2018-01-24 03:14:49

标签: php laravel session

我的问题是我想在登录后存储年度信息会话,这是我的登录表单 enter image description here

我运行php artisan make:auth然后修改了LoginController并覆盖了一些函数

protected $redirectTo = '/';
protected $year = 2017;

private function setYear($year)
{
    $this->year = $year;
}

private function getYear()
{
    return $this->year;
}

private function setPath()
{
    $this->redirectTo = route('dashboard', ['tahun' => $this->getYear()]);
}

private function getPath()
{
    return $this->redirectTo;
}


protected function validateLogin(Request $request)
{
    $this->validate($request, [
        $this->username() => 'required|string',
        'password' => 'required|string',
        'year' => 'required|string'
    ]);
}

protected function authenticated(Request $request, $user)
{
    $this->setYear($request->input('year'));
    $this->setPath();
    $this->setSession();

    return redirect()->intended($this->getPath());
}

protected function setSession()
{
    session(['year' => $this->getYear()]);
}

登录运行良好,但我无法从其他控制器获取年份信息。当我写dd(session('year'))时,它会返回null

1 个答案:

答案 0 :(得分:1)

来自文档:

// Via a request instance...
$request->session()->put('key', 'value');

// Via the global helper...
session(['key' => 'value']);

您实际上并未在会话中设置任何内容。您已为属性$year分配了一个值,但在请求结束时将丢失该值。

您需要知道的一切:https://laravel.com/docs/5.5/session#storing-data