所以我正在使用zend-framwork创建一个项目,我正在尝试实现flash messenger助手,但我找不到任何好的实践来实现它。 我需要的是使用flash messenger发送消息和重定向,而消息将直接出现在layout.phtml中的特定位置。 我知道,对于重定向器,我可以这样做:
$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
$redirector->gotoUrl('/my-controller/my-action/param1/test/param2/test2')
->redirectAndExit();'
我可以对闪光灯消息器做些什么来使其工作?那是什么最好的做法?
答案 0 :(得分: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后几乎每次都会重定向。
答案 1 :(得分:2)
如何在Zend中使用flash messenger 假设您有一个名为'foo'的行动
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 } } }?>
答案 2 :(得分:0)
这应该在Controller内部工作
/**
* @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;
}