会话 – zf2中的Flash消息
发布时间:2020-12-15 07:22:34 所属栏目:百科 来源:网络整理
导读:我怎样才能在zend freamwork 2中使用flash messenger?会话文档还没有.谁知道呢?但会话库就在那里. 解决方法 我前段时间写了一篇关于此的帖子.你可以找到它 right here 基本上你像以前一样使用它. ?phppublic function commentAction(){ // ... display For
我怎样才能在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示例).或者,您可以随时自由编写自己的视图帮助程序,使其在视图模板中看起来更清晰. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ruby-on-rails – 如何在Ruby on Rails中添加自定义路由,控
- postgresql – 复制Postgres表,同时保持主键自动增量
- react-native启动异常,react-deep-force-update/.babelrc
- cmake Xcode生成器创建一个无法构建的项目
- 如何在ruby中创建具有给定绑定的块?
- 用XMLHTTP?Post/Get?HTML页面时的中文乱码之完全Script解
- ruby-on-rails – rails,如何使用content_tag在helper中构建
- c# – 比较linq中的日期
- 第一遍机房收费系统总结
- Flex布局做出自适应页面--语法和案例