php 中的 C# TempData 等效项

发布于 2024-12-06 05:31:43 字数 538 浏览 0 评论 0原文

我知道我可以手动显式设置和取消设置会话,但我相信这是值得询问的。在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

装迷糊 2024-12-13 05:31:43

正如其他人所指出的,使用会话来启用 TempData。下面是一个简单的 PHP 实现:

class TempData {
    public static function get($offset) {
        $value = $_SESSION[$offset];
        unset($_SESSION[$offset]);
        return $value;
    }

    public static function set($offset, $value) {
        $_SESSION[$offset] = $value;
    }
}

测试:

TempData::set("hello", "world");
var_dump($_SESSION); // array(1) { ["hello"]=> string(5) "world" }

TempData::get("hello"); // => world
var_dump($_SESSION); // array(0) { } 

不幸的是,我们无法使用静态类实现 ArrayAccess。

As the others have pointed out, uses sessions to enable TempData. Here is a simple PHP implementation:

class TempData {
    public static function get($offset) {
        $value = $_SESSION[$offset];
        unset($_SESSION[$offset]);
        return $value;
    }

    public static function set($offset, $value) {
        $_SESSION[$offset] = $value;
    }
}

Test:

TempData::set("hello", "world");
var_dump($_SESSION); // array(1) { ["hello"]=> string(5) "world" }

TempData::get("hello"); // => world
var_dump($_SESSION); // array(0) { } 

Unfortunately, we can not implement ArrayAccess with a static class.

若水微香 2024-12-13 05:31:43

PHP 中没有这个功能,但自己实现它应该不会太难。实际实施取决于您的具体需求。

  • 您是否需要跨用户提供这些数据,或者为每个用户单独提供这些数据?
  • 您希望它有一个默认的过期时间吗?
  • 您是否希望它只出现在活动请求中,还是应该持续存在直到有人检索它?
  • “未命中”是否可以接受(请参阅 memcached),还是您希望确保在请求时找到数据?

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.

  • Do you need that data across users or separated for each user?
  • Do you want it to have a default expiration time?
  • Do you want it just in the active request or should it persist until someone retrieves it?
  • Are "misses" acceptable (see memcached) or do you want to be sure you find the data when you request it?
绝不放开 2024-12-13 05:31:43

正如 @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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文