加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

会话 – 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示例).或者,您可以随时自由编写自己的视图帮助程序,使其在视图模板中看起来更清晰.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读