简体   繁体   English

在存储功能中获取会话数据

[英]Get Session data in store function

Is it right to get Session data in a store function and store them into db? 在存储函数中获取Session数据并将其存储到db是否正确?

public function store(){    
  ...
  $idgroup = Session::get('invitation_userid')];
  ...
}

Or need a store function always a Request Object? 还是需要存储功能始终是请求对象?

public function store(Request $request){    
  ...
  $idgroup = $request('idgroup');
  ...
}

In both functions is of course a validation part for the input data. 在这两种功能中,当然都是输入数据的验证部分。

Both approaches are fine, but you should use them appropriately to your use case, I prefer to use the Request data. 两种方法都很好,但是您应该根据自己的用例适当使用它们,我更喜欢使用Request数据。 The main difference is that if u store that inside the Session it will be available application wide, while if u send inside Request it will be available inside the method only 主要区别在于,如果将其存储在Session ,它将在整个应用程序范围内可用;而如果您将其发送到Request ,则仅在方法内部可用。

This depends entirely on the context of what your controller is actually named, how this data is being used and why you are not using a database session driver in the first place if you want to do this. 这完全取决于您的控制器实际命名的上下文,此数据的使用方式以及为什么要这样做而不首先使用数据库会话驱动程序的原因。

You could simply use the database driver for the session: 您可以简单地使用数据库驱动程序进行会话:

https://laravel.com/docs/5.7/session#introduction https://laravel.com/docs/5.7/session#introduction

It also depends on what your controller is named if you strictly want to follow restful routes: 如果严格要遵循静态路由,这还取决于控制器的名称:

https://gist.github.com/alexpchin/09939db6f81d654af06b https://gist.github.com/alexpchin/09939db6f81d654af06b

To answer the second question you don't always need a Request object in your store action. 要回答第二个问题,您在store操作中并不总是需要一个Request对象。 Most of the time you won't even see a Request object because you are simply creating an entirely new resource. 在大多数情况下,您甚至都不会看到Request对象,因为您只是在创建全新的资源。

The Global Session Helper You may also use the global session PHP function to retrieve and store data in the session. 全局会话帮助器您还可以使用全局会话PHP函数来检索和存储会话中的数据。 When the session helper is called with a single, string argument, it will return the value of that session key. 当使用单个字符串参数调用会话帮助器时,它将返回该会话密钥的值。 When the helper is called with an array of key / value pairs, those values will be stored in the session: 当使用键/值对数组调用帮助程序时,这些值将存储在会话中:

$value = session('key');

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

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