如何将值从Controller传递到zend框架3中的视图?

时间:2016-09-19 23:32:49

标签: zend-framework zend-framework2 zend-framework3

我无法在Zend Framework 3中将值从Controller传递给View。

我尝试了以下操作:尝试在索引页面中打印$msg,它在索引页面上具有空值,但在add.phtml中它正确显示消息。

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

1 个答案:

答案 0 :(得分:1)

您将$ msg作为message传递给视图。因此,您需要使用message在视图中访问它。

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

或者像这样使用它:

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

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