zend-framework - 如何在 zend Framework 3 中使用 flash Messenger,因为 zf3 中删除了 View 助手。我找不到任何教程/示例来实现此功能

标签 zend-framework zend-framework3 flashmessenger

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 的骨架应用程序(专辑库存)。但无法成功

AlbumController.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);
}

index.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))
    ?>

最佳答案

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

所以在你看来:

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

更新:FlashMessenger 是一个旨在创建基于 session 的消息的插件。你滥用它了。它旨在在您的操作所使用的另一个 View 中显示消息。

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

关于zend-framework - 如何在 zend Framework 3 中使用 flash Messenger,因为 zf3 中删除了 View 助手。我找不到任何教程/示例来实现此功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39509354/

相关文章:

php - catch 语句中函数的错误处理

events - ZF3 - 附加到 EventManager 的事件未触发

php - Zend Framework 2 Flash Messenger 不返回消息

zend-framework - 在 Zend Framework 中找不到类

php - Zend_Gdata 和 OAuth

php - 使用 ZendSkeletonModule 创建新模块 ZF3

zend-framework2 - ZF3 - EventManager 和调度事件

zend-framework2 - 如何在 ZF2 FlashMessenger 中使用 HTML

php - 将 Doctrine 与 Zend Framework 1.8 应用程序集成