我正在尝试清除 CakePHP 3.1.1 中的 Flash 消息
当用户登录时,我有一个功能,如果他的客户数据不完整,他会被重定向到一个表单来完成它。看起来像这样:
public function index()
{
//Some code to check whether the customers profile is complete
//If it's not complete, then redirect to the "complete" action with a flash message
if($completion == 0){
$this->Flash->success(__('Please complete your customer profile data.'));
$this->setAction('complete', $custid);
//Otherwise go to their view
} elseif ($completion == 1){
$this->setAction('view', $custid);
}
}
这很好用,并且用户被重定向到带有 Flash 消息的完整操作/表单。
然后 Complete 操作如下所示:
public function complete($id = null)
{
//Get all the customer data input
if ($this->request->is(['patch', 'post', 'put'])) {
$customer = $this->Customers->patchEntity($customer, $this->request->data);
//Set the completion status to complete (1)
$customer->completion_status = 1;
if ($this->Customers->save($customer)) {
$completion = 1;
$this->set('completion', $completion);
//After the Save, redirect to the View action with a new Flash Message
$this->Flash->set(__('Your customer information is complete and has been saved!',['clear'=>'true']));
return $this->redirect(['action' => 'view',$custid]);
} else {
$this->Flash->error(__('The customer could not be saved. Please, try again.'));
}
}
$this->set(compact('customer'));
$this->set('_serialize', ['customer']);
}
它工作正常但是:当用户在保存数据后被重定向到带有 Success Flash 的 View 操作时,索引中的 Flash(告诉他们“请完成您的客户资料数据。”)仍然会再次出现。
如果用户在视图上刷新,那么两条 Flash 消息都会按原样消失。
重定向时如何清除该初始 Flash 消息?我试过使用清除键,但它似乎不起作用。
任何意见是极大的赞赏!谢谢, DBZ