无法将变量发送到视图

时间:2012-09-21 14:15:40

标签: php zend-framework

在我的控制器中,我尝试在将变量发送到视图之前将其保存在会话中:

session_start();
$_SESSION['prenom'] = $form['prenom'];

$this->_view->prenom = $_SESSION['prenom'];

$this->_redirect('inscription');

然后在我看来我需要显示它:

<div><input type="text" name="prenom" value="<?php if (isset($prenom)){echo $prenom;}?>" title="Pr&#233;nom *" class="small"/>

提前感谢您的帮助

4 个答案:

答案 0 :(得分:2)

首先让我们保持一致并使用Zend Framework。

//session_start();
//$_SESSION['prenom'] = $form['prenom'];  BECOMES
$session = new Zend_Session_Namespace('name'); //will automatically call session_start if required
$session->prenom = $form->getValue('prenom');//assumes $form is a valid Zend_Form object

//$this->_view->prenom = $_SESSION['prenom']; BECOMES
$this->view->prenom = $session->prenom; //$this->_view isn't a thing as far as I know
//if you are expecting the view variable to work after redirect... it won't
//$this->_redirect('inscription'); MAY BECOME
$this_forward('inscription');//will call new action without resetting the request

现在用于查看脚本:

//assumes you are in the correct view script
//when accessing variables passed to the view, use $this
<input type="text" name="prenom" value="<?php 
if (isset($this->prenom) { 
    echo $this->prenom;
}
?> " title="Prenom *" class="small"/>

答案 1 :(得分:1)

重定向方法重置设置变量。你必须改变你的观点。类似的东西:

$this->_helper->viewRenderer('action-name-here');

注意:如果您使用的是Zend Framework,请不要使用本机会话方法,请使用Zend_Session

答案 2 :(得分:0)

<div><input type="text" name="prenom" value="<?php if (isset($_SESSION['prenom'])) { echo $_SESSION['prenom']; }?>" title="Pr&#233;nom *" class="small"/>

答案 3 :(得分:0)

使用重定向方法时,会发生实际HTTP重定向(页面重新加载),因此所有变量都将被重置。我认为您正在寻找前向方法