How to pass values from Controller to view in zend framework 3?

I am having trouble passing values from Controller to View in Zend Framework 3.

I tried the following: tried to print $msg in index page, it is having null values on index page but in add.phtml it is displaying the message correctly.

AlbumController.php

public function addAction()
{
  $form = new AlbumForm();
  $msg ='hello';
  print_r($msg);
  $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()->addSuccessMessage('<b>Added Successfully...</b>');
          //$successMessages = $this->flashMessenger()->hasSuccessMessages() ? $this->flashMessenger()->getSuccessMessages() : null;
          //print_r($successMessages);exit;
      } 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,'success' => $successMessages);
  $view = new ViewModel(array('message' => $msg, 'form' => $form));
  return $view;
}

index.phtml

<?php
     echo($this->msg ? 'true' : 'false');
?>

Answers


You are passing $msg to the view as message. So you need to use message to access it in the view.

<?php echo($this->message ? 'true' : 'false');?>

Or use it like this:

// AlbumController.php
$view = new ViewModel(array('msg' => $msg, 'form' => $form));

// index.phtml
<?php echo($this->msg ? 'true' : 'false');?>

Need Your Help

disable select option value when selected

javascript jquery html html-select

I am using this code. But I'm facing a problem that when I select "one" in 1st select box and then in 2nd I select "two" and when I select again 2nd value to "three" but the value "two" remain disa...

Program compiled with gfortran, how to recompile with ifort?

compiler-construction compilation fortran gfortran intel-fortran

Okay, I have the source code for a large open-source program on a Linux HPC. There are two distinct systems that make up the HPC, arc1 and arc2, which share a filesystem. Essentially, I need two