2012-05-07 30 views
3

所以我創建一個項目使用zend-framwork,我想實現的Flash信使助手,但我找不到任何良好的做法來實現它。 我需要的是使用flash messenger發送消息並重定向,而消息將直接顯示在layout.phtml中的特定位置。 我知道,對於我重定向器可以做到這一點:zend框架閃信使消息和重定向

$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector'); 
$redirector->gotoUrl('/my-controller/my-action/param1/test/param2/test2') 
       ->redirectAndExit();' 

我可以做的閃光信使什麼,使其工作?那最好的做法是什麼?

回答

6

在你的控制器中

public function init() 
{ 
$messages = $this->_helper->flashMessenger->getMessages(); 
if(!empty($messages)) 
$this->_helper->layout->getView()->message = $messages[0]; 
} 

在layout.phtml

<!-- Global notification handling to use call flashMessenger action helper --> 
    <?php if(isset($this->message)) :?> 
    <div class="notification"> 

    <?php echo $this->message ;?> 

    </div>  
<?php endif;?> 

然後,每當你想用它

public function loginAction() 
{ 
$this->_helper->flashMessenger('Login is success'); 
$this->_helper->redirector('home'); 
} 

幾乎你會使用flashMessenger後重定向的每次。

+0

這隻會得到一個Flash消息一次..你不能迭代 – coolguy

+0

我從來沒有遇到任何需要多一條消息。 –

+0

這現在工作得很好,但我有一個問題,爲什麼自動完成net net不顯示自動完成,當我使用_helper,我該如何做呢? –

0

這應該一個控制器

/** 
* @var Zend_Controller_Action_Helper_FlashMessenger 
*/ 
protected $flashMessenger = null; 


/** 
* initalize flash messenger 
* 
* @return void 
*/ 
public function init() 
{ 
    $this->flashMessenger = $this->_helper->FlashMessenger; 
} 

/** 
* Action wich is redirectin.. and sets message 
* 
* @return void 
*/ 
public function actiononeAction() 
{ 
    $this->flashMessenger->addMessage('FLY HiGH BiRD!'); 
    $this->_redirect('/whatever/url/in/your/project'); 
} 

/** 
* display messages from messenger 
* 
* @return void 
*/ 
public function displayAction() 
{ 
    $myMessages = $this->flashMessenger->getMessages(); 
    $this->view->messages = $myMessages; 
} 
+0

控制器不應該保持狀態;因此,爲他們創造變數並不是好習慣。相反,只需使用'$ this - > _ helper-> flashMessenger',或者如果無聊輸入longhand,則將其按照$ flashMessenger = $ this - > _ helper-> flashMessenger進行分配。如果您未使用模型,請在請求中使用請求對象的狀態。 – shrikeh

2

如何使用閃光信使在Zend的 假設你有一個名爲動作 '富'

public function fooAction(){ 

$flashMessenger = $this->_helper->getHelper('FlashMessenger'); 
//some codes 
$flashMessenger->addMessage(array('error' => 'This is an error message')); 
$this->_redirect('/someothercontroller/bar'); 

} 
//someothercontroller/barAction 
public function barAction(){ 

$flashMessenger = $this->_helper->getHelper('FlashMessenger'); 
$this->view->flashmsgs = $flashMessenger->getMessages(); //pass it to view 

} 

在你看來部件內部工作

<?php if(isset($this->flashmsgs)) { ?> 
       <?php foreach($this->flashmsgs as $msg){ 
         foreach ($msg as $key=>$diserrors) { 
         if($key=="error"){?> 
     //do wat you want with your message 
<?php } } }?>