商店和登录Laravel后检索会话 [英] Store & retrieving session after login Laravel

查看:77
本文介绍了商店和登录Laravel后检索会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我想在登录后存储带有年份信息的会话,这是我的登录表格

My problem is I want to store session with year information after login, here is my login form

我已经运行php artisan make:auth,然后修改了LoginController并覆盖了某些功能

I've run php artisan make:auth then modified LoginController and overrride some functions

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

The login is running well, but I can't get year information from another controller. When I write dd(session('year')) it return null

推荐答案

来自文档:

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

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

您实际上没有在会话中进行任何设置.您已为属性$year分配了一个值,但该值将在请求结束时丢失.

You aren't actually setting anything in the session. You've assigned a value to the property $year but that will be lost at the end of the request.

您需要了解的所有信息: https://laravel.com/docs/5.5 /session#storing-data

Everything you need to know: https://laravel.com/docs/5.5/session#storing-data

这篇关于商店和登录Laravel后检索会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆