Yii 中 getFlash 和 setFlash 的问题

发布于 2024-09-30 08:53:50 字数 380 浏览 2 评论 0原文

好吧,我知道我要说的内容会非常模糊,但请听我说:

我想使用闪信,因为我喜欢它们的想法,但它们似乎并不总是适合我。

这是我的工作流程:

  1. 用户在页面 A 上创建组
  2. 我向其发送电子邮件通知 我为新组成员
  3. 设置了闪现消息“成功!”
  4. 我将用户重定向到新创建的组并显示“成功!”闪讯。

问题是在我重定向用户之后,闪现消息不再位于 $_SESSION 中。更奇怪的是,如果我去掉发送电子邮件通知的步骤,它确实有效。搞什么?

有其他人遇到过类似的 Flash 消息问题吗?有谁知道为什么发送电子邮件会影响即时消息?

我想此时我将坚持使用 setState 并在显示它们后手动将其删除。

Ok, I know what I'm about to say is going to be super vague, but hear me out:

I want to use flash messages because I like the idea of them, but they don't seem to always work for me.

Here is my workflow:

  1. User creates a group on page A
  2. I send an email notification to
    members of the new group
  3. I set the flash message "Success!"
  4. I redirect the user to the newly created group and display the "Success!" flash message.

The problem is after I redirect the user, the flash messages are no longer in the $_SESSION. To make this even more strange, if I take out the step where I send email notifications, it DOES work. WTF?

Has anyone else experienced similar issues with flash messages? Does anyone have any insight into why sending the email would affect the flash messages?

I think at this point I'm just going to stick with setState and manually remove it after displaying them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

少女净妖师 2024-10-07 08:53:50

现在您可以将 CWebUser 组件的属性 autoUpdateFlash 设置为 false。在这种情况下,闪烁消息仅在使用后才会被删除。

Now You can set a property autoUpdateFlash of CWebUser component to false. In this case flash message will be removed only after usage.

小红帽 2024-10-07 08:53:50

电子邮件是如何发送的?我知道在“调试”模式下使用 Yii Mail 扩展(我认为是一个 swiftmailer 包装器)使用 setFlash 方法,所以也许这会以某种方式清除数组......?

“flash”变量由 CWebUser 的 init() 方法更新,因此如果对 CWebUser 进行额外调用,则可能会清除 flash 变量。

考虑到您在电子邮件之后设置了闪光灯,我不确定这是如何发生的。除了“调试模式”之外(如果您只是使用 PHP 邮件方法,这不是问题),我唯一的猜测是发送邮件会在某处触发额外的重定向。

如果您安装了像 XDebug 这样的 PHP 调试器,我会在 getFlash 和 setFlash 代码中设置一个断点,然后看看发生了什么。

祝你好运!

How is the email being sent? I know using the Yii Mail extension (a swiftmailer wrapper I think) in "debug" mode uses the setFlash method, so perhaps that's wiping out the array somehow...?

The "flash" variables are updated by the init() method of CWebUser, so if there is an extra call to CWebUser it's possible that's what is clearing out the flash variables.

Considering that you set the flash AFTER the email though, I'm not sure how that would happen. My only other guess besides the "debug mode" thing (not a problem if you are just using the PHP mail methods though) is that sending mail is triggering an extra redirect somewhere.

If you have a PHP debugger like XDebug installed I would set a breakpoint in the getFlash and setFlash code and get a look at what is happening that way.

Good luck!

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