简体   繁体   English

如何在Laravel 5.5中的自定义外观中使用会话

[英]How to use session in custom facade in Laravel 5.5

I am new to Laravel and working on version 5.5. 我是Laravel的新手,正在研究5.5版。 For my knowledge, I have created custom facade and now I am trying to set some session values into this facade as well as try to get the value of set session. 就我所知,我已经创建了自定义外观,现在我试图在该外观中设置一些会话值,并尝试获取已设置会话的值。

But Laravel is not able to set session in this custom facade. 但是Laravel无法在此自定义外观中设置会话。 If I do the same thing in controller then it is working fine. 如果我在控制器中执行相同的操作,则工作正常。

Below are my possible way that I have tried: 以下是我尝试过的可能方法:

public function setUserSession() {  
   //Ist way
   session()->put('user', 'some values');
   var_dump(session()->has('user')); // this is returning me false  

   //2nd way  
   request()->session()->put('user', 'some values');  
   var_dump(request()->session()->has('user')); // this is returning me false 

   Session::set('user', 'some values');  //this is giving me error
   /* for above line I have include below namespace:  
      1.Illuminate\Support\Facades\Session  
   */  
   var_dump(Session::get('user')); // this is returning me false 
}  

Even I have tried the solutions of below link: 即使我尝试了以下链接的解决方案:

How to use session in custom class laravel 5? 如何在自定义类laravel 5中使用会话?

But nothing change. 但是什么都没有改变。 Please anyone share your solution for this issue? 请任何人分享您对此问题的解决方案?

Thanks in Advance. 提前致谢。

Custom facade has nothing to do with session. 自定义外观与会话无关。 Ensure the place where you call setUserSession() is in the route that applies web middleware because web middleware starts session service 确保调用setUserSession()的位置在应用web中间件的路由中,因为web中间件会启动会话服务

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

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