2

zf3 flash-messenger 的文档页面中提供了以下代码。我知道我必须将返回变量发送到查看,以便显示消息。

public function processAction()
{
    // ... do some work ...
    $this->flashMessenger()->addMessage('You are now logged in.');
    return $this->redirect()->toRoute('user-success');
}

public function successAction()
{
    $return = ['success' => true];
    $flashMessenger = $this->flashMessenger();
    if ($flashMessenger->hasMessages()) {
        $return['messages'] = $flashMessenger->getMessages();
    }
    return $return;
}

更新: 我正在尝试将 flash messenger 功能添加到 zf3 的骨架应用程序(专辑库存)中。但无法成功

专辑控制器.php

public function addAction()
{
  $form = new AlbumForm();
  $form->get('submit')->setValue('Add');
  $request = $this->getRequest();

  if ($request->isPost()) {
      $album = new Album();
      $form->setInputFilter($album->getInputFilter());
      $form->setData($request->getPost());
      $add = $request->getPost('submit', 'Cancel');
          if ($form->isValid()) {
              $album->exchangeArray($form->getData());
              $this->table->saveAlbum($album);
              $this->flashMessenger()->addMessage('<div class="alert alert-success" role="alert"><b>Added Successfully...</b></div>');
      } else {
          $this->flashMessenger()->addMessage('<div class="alert alert-danger" role="alert"><b>Failed to Add...!!</b></div>');
      }

      return $this->redirect()->toRoute('album');
  }
  return array('form' => $form);
}

索引.phtml

<?php
$search="";
$title = 'My albums';
$this->headTitle($title);
$url_order = 'ASC';
if ($order_by == 'title')
  $url_order = ($order == 'ASC') ? 'DESC' : 'ASC';
elseif ($order_by == 'artist')
  $url_order = ($order == 'ASC') ? 'DESC' : 'ASC';
if(!empty($search_by))
   $search=$search_by;
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>
<p><a href="<?php echo $this->url('album', array('action'=>'add'));? >">Add new album</a>  </p>

<?php
$form  = $this->form;
$form->setAttribute(($this->url('album', array('order_by' => $order_by,   'order' => $url_order))),$search);
$form->prepare();
echo $this->form()->openTag($form);
foreach ($form as $element) :
?>
<div class="control-group <?php if ($this->formElementErrors($element)) echo "error" ?>">
    <label class="control-label"><?php echo $element->getLabel() ?></label>
    <div class="controls">
        <?php
        echo $this->formElement($element);
        if ($this->formElementErrors($element)):
            ?>
            <span class="help-inline"><?php echo $this  >formElementErrors($element); ?></span>
            <?php
        endif;
        ?>
    </div>
 </div>
 <?php
endforeach;
echo $this->form()->closeTag();
?>
<table class="table">
<tr>
 <th><a href="<?php echo $this->url('album', array('order_by' => 'title', 'order' => $url_order), array('query' => $search)); ?>">Title<?php if ($order_by == 'title'): ?><i class="icon-chevron-<?php echo $url_order == 'ASC' ? 'down' : 'up' ?>"></i><?php endif; ?></a></th>
 <th><a href="<?php echo $this->url('album', array('order_by' => 'artist', 'order' => $url_order),array('query' => $search)); ?>">Artist<?php if ($order_by == 'artist'): ?><i class="icon-chevron-<?php echo $url_order == 'ASC' ? 'down' : 'up' ?>"></i><?php endif; ?></a></th>
 <th>Action</th>
 <th>&nbsp;</th>
 </tr>
 <?php foreach ($this->paginator as $album) : ?>
 <tr>
    <td><?= $this->escapeHtml($album->title) ?></td>
    <td><?= $this->escapeHtml($album->artist) ?></td>
    <td>
        <a href="<?= $this->url('album', ['action' => 'edit', 'id' => $album->id]) ?>">Edit</a>
        <a href="<?= $this->url('album', ['action' => 'delete', 'id' => $album->id]) ?>">Delete</a>
    </td>
    </tr>
  <?php endforeach; ?>
  </table>
  <?=
  $this->paginationControl(
  $this->paginator,
  'sliding',
  'partial/paginator.phtml',
  array('order_by' => $order_by, 'order' => $order, 'search_by' => $search,'pageAction' => $pageAction))
    ?>
4

1 回答 1

2

因此,如果您查看刚刚发布的示例,在 上successAction(),示例检查 FlashMessenger 是否有消息,如果有,它会将消息传递给名为messages(根据需要命名)的变量中的视图。

所以在你看来:

<?php if (isset($messages)): ?>
    <ul>
    <?php foreach($messages as $message): ?>
        <li><?= $message ?></li>
    <?php endforeach; ?>
    </ul>
<?php endif; ?>

编辑消息后更新FlashMessenger:是一个插件,旨在创建基于会话的消息。你在滥用它。它旨在在您的操作使用的另一个视图中显示消息。

在您的示例中,您不需要,FlashMessenger因为您只显示在同一页面中定义的消息。只需使用将在视图中使用的数组...

于 2016-09-15T14:25:17.343 回答