我无法在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');
?>
答案 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');?>