php 中的 C# TempData 等效项
我知道我可以手动显式设置和取消设置会话,但我相信这是值得询问的。在 C# 中,有一个名为 TempData 的字典,它存储数据直到第一个请求。换句话说,当调用 TempData 时,它会自动取消设置。为了更好地理解,这里有一个示例:
Controller1.cs:
TempData["data"] = "This is a stored data";
Model1.cs:
string dst1 = TempData["data"]; // This is a stored data
string dst2 = TempData["data"]; // This string will be empty, if an exception is not raised (I can't remember well if an exception is raised)
所以基本上,这只是一个仅供 1 使用的会话。再说一遍,我知道我可以在 php 中显式设置和取消设置,但是 php 是否有这样的函数?
I know I can explicitly set and unset a Session manually but I believe this is worth to ask. In c#, there is a dictionary called TempData which stores data until the first request. In other words, when TempData is called, it is automatically unset. For a better understanding here is an example:
Controller1.cs:
TempData["data"] = "This is a stored data";
Model1.cs:
string dst1 = TempData["data"]; // This is a stored data
string dst2 = TempData["data"]; // This string will be empty, if an exception is not raised (I can't remember well if an exception is raised)
So basically, this is just something like a session for 1 use only. Again, I know that I can set and unset explicitly in php, but still, does php has a function like this one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如其他人所指出的,使用会话来启用 TempData。下面是一个简单的 PHP 实现:
测试:
不幸的是,我们无法使用静态类实现 ArrayAccess。
As the others have pointed out, uses sessions to enable TempData. Here is a simple PHP implementation:
Test:
Unfortunately, we can not implement ArrayAccess with a static class.
PHP 中没有这个功能,但自己实现它应该不会太难。实际实施取决于您的具体需求。
You don't have that in PHP, but it shouldn't be too hard to implement it yourself. The actual implementation depends on your exact needs.
正如 @AVD 所说,没有这样的命令。我真的不明白为什么。 TempData 的特点是它允许您保存一些值/对象以便往返服务器。
如果您确实在网站中使用会话,则不使用会话来存储这些值没有问题。会话存储放在服务器上,用户通过每次发送到服务器的sessionid来标识。
我能看到的唯一性能损失是,如果您要在运行 http 处理程序的进程之外运行会话存储。否则它们都在内存中并且应该很快。
As @AVD tells, there is no such command. And I can't really see why. The thing with TempData is that it allows you to save some values / objects for a roundtrip to the server.
If you do use Sessions in your website there is no issue to not use Session to store these values. Session storage is placed on the server and the users is identified by a sessionid which is sent to the server each time.
The only performance penalty I can see is if you were to run your session storage outside your process running the http handler. Otherwise they are both in memory and should be pretty fast.