我运行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
答案 0 :(得分:1)
来自文档:
// Via a request instance...
$request->session()->put('key', 'value');
// Via the global helper...
session(['key' => 'value']);
您实际上并未在会话中设置任何内容。您已为属性$year
分配了一个值,但在请求结束时将丢失该值。