Chaitanya Sairam
Chaitanya Sairam

Reputation: 131

How to use flash messenger in zend framework 3,since view helper is removed in zf3.I couldn't find any tutorials/examples to implement this

The following code is provided in the documentation page of zf3 flash-messenger.I know i have to send the return variable to view, inorder to display the messages.

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;
}

UPDATE: I am trying to add flash messenger functionality to skeleton application(Album Inventory) of zf3.But could not succeed

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

Upvotes: 2

Views: 3847

Answers (1)

Al Foиce    ѫ
Al Foиce ѫ

Reputation: 4315

So if you have a look at the example you just post, on the successAction(), the example check if FlashMessenger has message, and if yes, it passed the messages to the view in a variable called messages (named it as you want).

So in your view:

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

UPDATE after you edit your message: FlashMessenger is a plugin designed to create session-based messages. You are misusing it. It is designed to display messages in another view that the one used by your action.

In your example, you don't need FlashMessenger because you just display messages defined in the same page. Just use an array that you will use in the view...

Upvotes: 2

Related Questions