简体   繁体   English

Laravel:如何在整个应用程序中使用会话?

[英]Laravel: How to use session in the whole app?

I want use session in Laravel like $_SESSION['test'] in a normal PHP. 我想在Laravel中像$_SESSION['test']在普通的PHP中使用会话。

I tried this in Laravel, but the data is not stored when I use the session in another Controller. 我在Laravel中尝试了此操作,但是当我在另一个Controller中使用该会话时,数据没有存储。

// User CONTROLLER //用户CONTROLLER

class UserController extends Controller
{
    public function testSession(Request  $request)
    {
       // ...
    Session::set( 'id','1' );
    $request->session()->put('username', 'simo');

    }
}

Another CONTROLLER 另一个控制器

class AnotherController extends Controller
{
    public function GetMySession(Request  $request)
    {
       // ...
    dd($request->session()->all());// 

    }
}

Now username and id are not stored in the Session. 现在,用户名和ID未存储在会话中。 Why? 为什么?

In laravel,To use sessions use Session; 在laravel中,要使用会话,请use Session;

When you want to set a session use set() method as - 如果要设置会话,请使用set()方法为-

Session::set('variableName', $value);

When you want to get any session then get it like this - 当您想要进行任何会话时,请按以下方式进行操作-

Session::get('variableName');

And if you want to reset it again then use put() method - 如果要再次重置它,请使用put()方法-

Session::put('variableName', $value);

Hope this will help you. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM