簡體   English   中英

登錄后存儲和檢索會話Laravel

[英]Store & retrieving session after login Laravel

我的問題是我想在登錄后存儲帶有年份信息的會話,這是我的登錄表格 在此處輸入圖片說明

我已經運行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

從文檔:

// 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM