次のコードは、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;
}
更新: 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> </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))
?>