2016-01-09 34 views
0

我試圖清除閃爍留言CakePHP中3.1.1清除閃爍留言CakePHP中3.1.1

我有一個功能,當用戶登錄時,如果他的客戶資料不完整,他被重定向以一種形式來完成它。看起來像這樣:

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消息的完整操作/表單。

然後將整個動作看起來是這樣的:

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']); 
} 

它工作正常,但:保存自己的數據後,當用戶被重定向到與成功的Flash的瀏覽動作,Flash的指數(告訴他們'請填寫您的客戶資料數據')仍會再次顯示。

如果用戶在視圖上刷新,則兩個Flash消息都應該消失。

如何在重定向時清除最初的Flash消息?我試過使用清除鍵,但它似乎不工作。

任何意見非常感謝! 謝謝, DBZ

回答

2

顯信息都存儲在會話,所以只是清除相關的會話密鑰:$this->Session->delete('Flash.flash')$this->Session->delete('Flash')

+0

感謝您的回覆。但是,這些給我的錯誤:「調用一個非對象的成員函數delete()」。 – dividedbyzero

+0

此外,當在調試模式下,會話Flash變量顯示爲空....不知道爲什麼這是... – dividedbyzero

+0

在cake3其$ this-> request-> session() - >刪除 –

0

顯然要傳遞一個字符串,明確,而不是一個布爾值:

3.1版新增功能:添加了新的密鑰清除功能。這個鍵需要一個bool,並且允許你刪除當前棧中的所有消息並開始一個新的消息。

嘗試設置真正不帶引號:

$this->Flash->set(__('Your customer information is complete and has been saved!'),[ 
    'clear'=> true 
]); 
+0

謝謝道格拉斯。我按照你的建議刪除了引號,但我仍然得到相同的行爲。我也嘗試設置值爲'清除'=> 1,仍然沒有運氣.... – dividedbyzero

+0

我剛剛意識到你有一個語法錯誤。試試這個:'$ this-> Flash-> set(__('你的客戶信息已經完成並且已經保存!'),['clear'=> true]);'圓括號並沒有關閉。 –

+0

嗨道格拉斯....這是它! Darn ...非常感謝你。修正了括號,現在它起作用了。我欠你一個! – dividedbyzero

0

檢查以確保您不會總是得到$completion == 0(可以匹配FALSE爲好)。

我懷疑這就是爲什麼你的Flash消息總是顯示。

Cake在顯示它時會自動刪除一條Flash信息。

+0

謝謝。不,不是的。變量顯示正確設置。即使我將Flash消息移動到「完成」操作的開始位置,在執行另一操作後它仍然存在。 – dividedbyzero

0

版本3.1中的新功能:Flash消息現在堆疊。使用同一個鍵連續調用set()或__call()會將消息附加到$ _SESSION中。如果您想要保留舊的行爲(即使在連續調用後仍有一條消息),請在配置組件時將clear參數設置爲true。

使用這樣的:

$this->loadComponent('Flash', ['clear' => true]); 
0

您可以在動作的開頭添加這

$this->request->session()->delete('Flash'); 

刪除更具體的你可以做如從AuthComponent只刪除郵件

$this->request->session()->delete('Flash.auth'); 

一兩件事: 你可以控制的閃光燈信息顯示在類似的觀點:

this->Flash->render('auth'); 

this->Flash->render('error'); 

如果您不要在會話中顯示它的閃光消息,直到您將它顯示在某個地方或從會話中刪除它。