开发者

need help understanding getRequest() Zend Framework

开发者 https://www.devze.com 2023-01-04 21:32 出处:网络
I\'m doing this tutorial: http://www.phpeveryday.com/articles/Zend-Framework-Database-Creating-Input-Form-P494.html

I'm doing this tutorial: http://www.phpeveryday.com/articles/Zend-Framework-Database-Creating-Input-Form-P494.html

We are building a simple input form using POST and submitting it to a mySQL database. All is working fine. I'm just trying to get my head around the getRequest() function.

In the controller, we have this:

public function registerAction()
 {
    $request = $this->getRequest();

    $this->view->assign('action',"process");
    $this->view->assign('title','Member Registration');
    $this->view->assign('label_fname','First Name');
    $this开发者_StackOverflow中文版->view->assign('label_lname','Last Name'); 
    $this->view->assign('label_uname','User Name'); 
    $this->view->assign('label_pass','Password');
    $this->view->assign('label_submit','Register');     
    $this->view->assign('description','Please enter this form completely:');        
}

and then in view:

 <form name="register" method="post" action="<?php echo $this->escape($this->action)?>">
  <table>
    <tr>
      <td><?php echo $this->escape($this->label_fname)?></td>
      <td><input type="text" name="first_name"></td>
    </tr>
    <tr>
      <td><?php echo $this->escape($this->label_lname)?></td>
      <td><input type="text" name="last_name"></td>
    </tr>   
    <tr>
      <td><?php echo $this->escape($this->label_uname)?></td>
      <td><input type="text" name="user_name"></td>
    </tr>   
    <tr>
      <td><?php echo $this->escape($this->label_pass)?></td>
      <td><input type="password" name="password"></td>
    </tr>   
  </table>
  <input type="submit" name="submit" value="<?php echo $this->escape($this->label_submit);?>">
  </form>

So what I don't understand is why do we need a getRequest() if I already have the method="post" and the action set? If I comment it out, the script doesn'twork. I see it's needed, but I don't understand why-especially since the $request variable doesn't seem to be used?


In the code you have provided, $request doesn't appear to be used at all. I don't see why commenting it out would break.

What happens, exactly, when you comment it out?

The getRequest() function is for getting the Request object, which gives you parameters and such (i.e. controller, action, etc);

EDIT:

I had a look at the tutorial, and it has this:

12   
13    $sql = "INSERT INTO `user`
14            (`first_name` , `last_name` ,`user_name` ,`password`)
15            VALUES
16            ('".$request->getParam('first_name')."', '".$request->getParam('last_name')."', '".$request->getParam('user_name')."', MD5('".$request->getParam('password')."'))";
17    $DB->query($sql);

You'll note that it uses the $request variable to get the Parameters: 'first_name', 'last_name', 'user_name', 'password'

And saves them into the DB.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号