2013-10-21 26 views
0

在我的控制器中使用$this->Session->setFlash('Some bog standard message.')時,閃光消息按預期正確顯示。默認樣式是紅色的,看起來像一個錯誤。 In the documentation它說明了可以通過將字符串傳遞給$key參數來更改Flash消息的嚴重性。該文檔使用「好」和「壞」,但調整的代碼時,以下幾點:CakePHP setFlash()嚴重性('好'/'壞')不工作?

$this->Session->setFlash('A good message!', 'default', array(), 'good'); 

沒有閃光燈消息可言,甚至沒有默認的紅色。我必須這樣做才能獲得理想的效果:

$this->Session->setFlash('A good message!', 'default', array('class' => 'message success')); 

這是壞了還是我錯過了什麼?

+1

這是預期的行爲,正如文檔描述,使用'key'參數時,你必須使用適當的在視圖中也是'key',也就是'$ this-> Session-> flash('good')'。 – ndm

回答

2

的你如何能書中引用做到這一點說明:

如果你想要的顏色改變,你需要以不同的樣式。他們在書中引用的關鍵只顯示你如何寫不同的提示信息 - 它無關,與該消息的實際造型 - 只是其中之一被寫入其中:

//Controller 
// set a bad message. 
$this->Session->setFlash('Something bad.', 'default', array(), 'bad'); 

// set a good message. 
$this->Session->setFlash('Something good.', 'default', array(), 'good'); 

// in a view. 
echo $this->Session->flash('good'); 
echo $this->Session->flash('bad'); 

所以 - 如果你想爲了使它與衆不同,你可以用div或者其他東西來包裝「好」,然後給它一個類,然後用不同的方式來設計它。使用你的想象力 - 但重點是,關鍵不會改變它的視覺風格,但是它是一個關鍵/它應該被寫入。

我怎麼辦呢 - 使用元素和參數來改變視覺樣式:

下面是我如何設置我的報警消息 - 這個使用Twitter的引導類和圖標更改警告根據的樣子在'type'參數上發送給我的「notifications」元素。當然,它可以根據您的需求進行編輯,但您應該瞭解另一條可以走的路線。

$this->Session->setFlash(__('This is my success message'), 
    'notifications', array('type'=>'success')); 

$this->Session->setFlash(__('This is my failure message'), 
    'notifications', array('type'=>'fail')); 

//'notifications' element 
<?php 
$icon = 'icon-ok-sign'; 
if(!empty($type)) { 
    switch($type) { 
     case 'fail': 
      $type='danger'; 
     case 'danger': 
      $icon = 'icon-warning-sign'; 
      break; 
     case 'info': 
      $icon = 'icon-info'; 
      break; 
     case 'success': 
      $icon = 'icon-ok-sign'; 
      break; 
     case 'error': 
      $type = 'danger'; 
      $icon = 'icon-warning-sign'; 
      break; 
    } 
} 

?> 
<div class="alert alert-<?php echo $type ?>"> 
    <button type="button" class="close" data-dismiss="alert">×</button> 
    <i class="<?php echo $icon ?>"></i> <?php echo $message; ?> 
</div> 
+0

天啊,我只是誤解了它!好的,我現在明白了,謝謝你的解釋。 – igneosaur

0

我個人創建了警報視圖元素,然後在我的setFlash()調用中傳遞類名。

<!-- View/Elements/flash.ctp --> 
<div class="alert alert-<?php echo $class; ?>"> 
    <p><?php echo $message; ?></p> 
</div> 

然後設置你的閃存的消息在控制器如下:

public function add() { 
    if ($this->request->is('post')) { 
     if ($this->Model->save($this->request->data)) { 
      $this->Session->setFlash('Lorem ipsum.', 'flash', array('class' => 'success')); 
     } 
    } 
} 
+0

1)我不相信這回答了他「爲什麼不工作」的問題,以及2)我已經完全解釋了它。 – Dave