为什么 CakePHP 设置 _method 等于 PUT 而不是 POST

Pat*_*iel 2 php cakephp

我最近更新了我的 CakePHP 项目到 2.4.5。

现在,一些forms设置输入 hidden = PUT。但是,这method是POST。

我不知道为什么会这样。

这是表格:

  <?php echo $this->Form->create('User', array('url' => array('action' => 'new_password', $this->request->data['User']['forget_password'], 'admin' => false), 'autocomplete' => 'off')) ?>

    <?php echo $this->Form->hidden('User.id') ?>

    <fieldset>
      <label class="block clearfix">
        <span class="block input-icon input-icon-right">
          <?php echo $this->Form->password('User.password', array('label' => false, 'div' => false, 'class' => 'form-control', 'placeholder' => 'Digite a nova senha')) ?>
          <i class="icon-user"></i>
        </span>
      </label>

      <label class="block clearfix">
        <span class="block input-icon input-icon-right">
          <?php echo $this->Form->password('User.password_confirmation', array('label' => false, 'div' => false, 'class' => 'form-control', 'placeholder' => 'Digite novamente a nova senha')) ?>
          <i class="icon-user"></i>
        </span>
      </label>

      <div class="space"></div>

      <div class="clearfix">
        <?php echo $this->Form->button('<i class="icon-key"></i> '. __('Enviar'), array('class' => 'width-35 pull-right btn btn-sm btn-success', 'escape' => false)) ?>
      </div>

      <div class="space-4"></div>
    </fieldset>
  <?php echo $this->Form->end() ?>
Run Code Online (Sandbox Code Playgroud)

而且,行动:

/**
 * new_password method
 *
 * @access public
 * @param String $forget_password
 * @return void
 * @since 1.0 
 * @version 1.0 
 * @author Patrick Maciel
 */
public function new_password($forget_password)
{

  $user = $this->User->findByForgetPassword($forget_password);

  if ($user == false) {
    $this->Session->setFlash(__('Link inválido'), 'flash/frontend/error');
    $this->redirect('/');
  }

  $this->layout = 'login';

  if ($this->request->is('post')) {
    $this->User->set = $this->request->data;
    if ($this->User->validates(array('fieldList' => array('id', 'forget_password', 'password', 'password_confirmation')))) {

     // ...

    } else {

     // ...

    }
  }

  $user['User']['password'] = null;
  $this->request->data = $user;

}
Run Code Online (Sandbox Code Playgroud)

所以...

  • 为什么 CakePHP 会这样做?
  • 我可以,如果我将我的方法更改为验证$this->request->is('put')而不是 POST 将起作用,但是,我不想这样做。我想要一个 POST,而不是一个 PUT。
  • 我需要知道,CakePHP 如何以及何时在input hidden form.

观察:我不强制方法,使用'type' => 'POST'因为,以前这是不需要的。

对不起我的英语不好。

Rob*_*ado 5

正如我从文档中可以推断出的那样,当您向视图提供数据并基于此数据创建表单时,Cake 假定您要编辑表单,因此它使其成为“放置”请求。

PUT方法来自 REST 服务,该方法与编辑内容相关联,而不是POST用于插入新内容的方法。因此,当 Cake 看到传递给视图的数据时,它会将其解释为正在编辑它。

因此,如果您想通过邮寄方式接收此表单,您有两个选择:通过传递 option 更改表单方法'type' => 'post',或通过以下方式更改控制器if($this->request->is('put'))

查看文档以获取更多参考:http : //book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-create