我怎样才能在zend freamwork 2中使用flash messenger?会话文档还没有.谁知道呢?但会话库就在那里.
解决方法
我前段时间写了一篇关于此的帖子.你可以找到它
right here
基本上你像以前一样使用它.
<?php public function commentAction() { // ... display Form // ... validate the Form if ($form->isValid()) { // try-catch passing data to database $this->flashMessenger()->addMessage('Thank you for your comment!'); return $this->redirect()->toRoute('blog-details'); //id,blabla } } public function detailsAction() { // Grab the Blog with given ID // Grab all Comments for this blog // Assign the view Variables return array( 'blog' => $blog,'comments' => $comments,'flashMessages' => $this->flashMessenger()->getMessages() ); }
然后在.phtml文件中,你这样做:
// details.phtml <?php if(count($flashMessages)) : ?> <ul> <?php foreach ($flashMessages as $msg) : ?> <li><?php echo $msg; ?></li> <?php endforeach; ?> </ul> <?php endif; ?>
显然这不是太方便,因为你必须为每个.phtml文件执行此操作.因此,在布局中执行此操作时,您最多只需执行以下操作:
<?php // layout.phtml // First get the viewmodel and all its children (ie the actions viewmodel) $children = $this->viewModel() ->getCurrent() ->getChildren(); $ourView = $children[0]; if (isset($ourView->flashMessages) && count($ourView->flashMessages)) : ?> <ul class="flashMessages"> <?php foreach ($ourView->flashMessages as $fMessage) : ?> <li><?php echo $fMessage; ?></li> <?php endforeach; ?> </ul> <?php endif; ?>
如果你需要进一步的描述,请看我的博客,但我猜代码本身很清楚(除了layout.phtml示例).或者,您可以随时自由编写自己的视图帮助程序,使其在视图模板中看起来更清晰.