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> </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))
?>