首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么CakePHP设置_method等于PUT而不是POST

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

Stack Overflow用户
提问于 2014-01-20 16:05:14
回答 2查看 3.5K关注 0票数 2

我最近将我的CakePHP项目更新为2.4.5。

现在,一些forms设置了输入隐藏= PUT。但是,method是POST。

我不知道为什么会发生这种事。

以下是表格:

代码语言:javascript
复制
  <?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() ?>

而且,行动:

代码语言:javascript
复制
/**
 * 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;

}

所以..。

  • 为什么CakePHP会这么做?
  • 我可以,如果我改变我的方法来验证$this->request->is('put'),而不是POST将工作,但是,我不想这样做。我要的是职位,而不是推荐信。
  • 我需要知道CakePHP如何以及何时在input hidden form中放置、发布或删除。

Obs.:我不强制方法,使用,因为以前不需要.

对不起我的英语。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-20 17:06:22

正如我可以从文档中推断的那样,当您向视图提供数据并基于这些数据创建表单时,Cake假设您希望编辑该表单,因此它使它成为一个"put“请求。

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

因此,如果您希望通过post接收此表单,则有两个选项:通过传递选项'type' => 'post'更改表单方法,或通过if($this->request->is('put'))更改控制器上的操作。

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

票数 5
EN

Stack Overflow用户

发布于 2014-10-21 19:18:31

就像Patrick说的,当Request data包含一个Model.id,CakeRequest::method()被设置为put。在cakephp中处理此问题的首选方法如下所示。

代码语言:javascript
复制
if ($this->request->is(array('post', 'put')) {
    // Code
}

您可以在烘烤的控制器中看到这一点,编辑操作。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21238682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档