【问题标题】:session flash message in cakephp2.xcakephp2.x 中的会话闪存消息
【发布时间】:2025-12-24 20:00:14
【问题描述】:

我在 cakephp 方面没有太多经验。 我在控制器中设置了一个会话闪存消息,其语法如下

$this->Session->setFlash(__('Error :: No product found.', true));

i have two function in Controller 
public function details()
{

}
public function add()
{
//my code goes here
$this->request->data['Product']['togo'] == the path where i have to redirect ( details page)
$this->Session->setFlash(__('updated.', true));
$this->redirect($this->request->data['Product']['togo']);
}

Now in the view folder
i dont have add.ctp file
add function manupulate the data and send them to the details page


details.ctp

Noe in the above page i have write below to display
 <?=$this->Session->flash();  // ?>

Issue 
when i click back or next button in the browser that session message will come on the screen

现在此消息也正确显示在视图文件中。现在的问题是 假设我将单击浏览器的 BACK 按钮,然后加载页面并再次显示该消息。如果我按下浏览器的 NEXT 按钮,那么也会显示该会话闪存消息。 我不知道为什么我会遇到这个问题。我有另一个项目,这个功能工作正常。 请帮助我摆脱这个问题

提前感谢

【问题讨论】:

  • 您能否提供更多代码快照?

标签: php variables cakephp cakephp-2.0 session-variables


【解决方案1】:

使用重定向

$this->redirect(array('controller'=>'Controller1','action'=>'view_name'));

或 在 BACK 按钮上使用hyperlink

【讨论】:

  • 在视图中 $this->session->flash(); ?>
  • 我终于找到了一些东西。只需检查此链接:nagarmehul.com/development/cakephp/admin/employees/index/page:1 用户名和传递都是 .admin'。首先登录并编辑任何用户。现在单击浏览器中的后退按钮,然后单击浏览器的下一步按钮。对浏览器 Chrome 和 Firefox 执行相同的操作。在 Firefox 中它可以正常工作,但在 chrome 中它无法正常工作。那么任何人都可以建议我如何解决这个浏览器会话闪烁消息问题?
  • 两个浏览器都使用了哪个版本。在我的网站中,这两种浏览器都可以正常工作
【解决方案2】:

你可以试试这个

在控制器闪存消息中,您可以定义您想要的消息名称,例如saveorerr,这些消息写入闪存消息,它将自动连接控制器闪存。 并且您还可以在 Flash 消息中使用 in 数组定义您的自定义 css。

  return  $this->Session->setFlash('Succesfully updated','default',array('class'=>'alert alert-danger'),'saveorerr');

  <?php echo $this->Session->flash('saveorerr');?>

【讨论】:

  • 首先感谢但问题是 Flash 消息工作正常。但是当我点击浏览器中的上一个然后下一个按钮时,它会再次显示。只需首先我单击浏览器中的按钮,然后单击浏览器中的下一个按钮,它会再次出现,所以我该如何解决这个问题。请帮助摆脱这个问题
  • 如果你定义了空的flash消息$this-&gt;Session-&gt;flash(),那么在同一控制器的一个视图.ctp文件中,消息会以不同的形式再次显示,所以请你可以定义不同的消息闪光灯的名称。
  • 我按照你告诉我的做了,但它现在仍在工作。如果我单击返回,然后单击浏览器上的下一步按钮,网站上仍会显示闪烁消息
  • 你能在controllerctp文件中显示你的会话代码吗?