在zend中将数据从视图传递到控制器

时间:2014-10-08 05:36:18

标签: php zend-framework2

我正在使用zend中的忘记密码模块。我想要' memcode'要发送给控制器的用户,以便我可以通过查询重置密码。

ForgotController.php

 public function changepasswordAction()
    {
    $post = $this->request->getPost();
    $dbAdapter=$this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
    $form = new ForgotForm($dbAdapter);
    $project = new Projects();
    $id=$this->params()->fromQuery('id');
    $viewModel = new ViewModel(array('form' =>$form));
    return $viewModel;

    }



      public function resetAction()
        {
        $pswd = $this->getRequest()->getPost('password');
        $id=$this->params()->fromRoute('id',0);
        echo $id;
        $sql="UPDATE projects set password = '".$pswd."' where memcode = '".$id."'";
        $statement = $dbAdapter->query($sql);
        $result    = $statement->execute();
        }

查看代码:changepassword.phtml

echo $_GET['id'];
$form = $this->form;
$form->setAttribute('action', $this->url(NULL,array('controller'=>'Forgot', 'action' =>'reset','id'=>$_GET['id'])));
$form->setAttribute('method', 'post');
$form->prepare();
echo $this->form()->openTag($form);?>
<h4>Set new password</h4>
<p> Welcome! <?php //echo $this->user_email; ?> </p>
<table>
<tr>
<td><dt><?php echo $this->formLabel($form->get('password')); ?></dt>
<dd><?php
echo $this->formElement($form->get('password'));
echo $this->formElementErrors($form->get('password'));
?></dd></td></tr>

<tr>
<td><dt><?php echo $this->formLabel($form->get('confirm_password')); ?></dt>
<dd><?php
echo $this->formElement($form->get('confirm_password'));
echo $this->formElementErrors($form->get('confirm_password'));
?></dd></td></tr>

<tr>
<td><dt><?php echo $this->formElement($form->get('submit'));
echo $this->formElementErrors($form->get('submit'));
?></dd></td></tr>

</table>
</dd>
</section>

如何将我通过的ID#34; $ _ GET [&#39; id&#39;]传递给控制器​​resetAction函数。现在我将O作为其值?

1 个答案:

答案 0 :(得分:0)

使用ZF2时不要直接使用$ _GET,您可以在changePasswordAction中执行此操作:

$viewModel = new ViewModel(array('form' => $form, 'id' => $id));

然后在您的changepassword.phtml中,您可以直接将此变量用作$this->id

关于你的代码的另一个注意事项:你应该小心这样的SQL,它可能容易受到SQL注入......