【问题标题】:POST dosn't work in yii2 restful APIPOST 在 yii2 restful API 中不起作用
【发布时间】:2015-09-07 00:03:19
【问题描述】:

我用 yii2 restful API 解释听说: http://www.yiiframework.com/doc-2.0/guide-rest-quick-start.html

在 CURL 中,它可以正确地用于 GET 和 DELETE。 但是当我在 CURL 中使用它时:

curl -i -H "Accept:application/json" -H "Content-Type:application/json" 
-XPOST "my localhost path/customers"
-d '{"name": "xxxx", "family": "xxxx","mobile":"xxxxx","home":"xxxxx","work":"xxxx"}'

它仅在我的表“客户”中插入自动递增字段“id”,而我数据库中的其他字段为空!

【问题讨论】:

    标签: php rest yii2


    【解决方案1】:

    我认为这与 API 功能无关,但可以肯定这是您模型中的“安全属性”问题。

    请查看this piece of documentation 以确保您已将所有其他属性标记为安全。即使通过 REST 调用,这仍然是一个批量分配,遵循相同的规则。

    基本上,您要么将它们作为safe(至少)添加到验证规则中,要么将它们作为safeAttributes()-result 的一部分返回。

    【讨论】:

      【解决方案2】:

      谢谢。 我在我的模型类“客户”中添加了以下代码,它可以工作:

      public function rules()
      {
          return [
          [['name', 'family', 'mobile', 'home', 'work'], 'required'],
          [['name', 'family'], 'string', 'max' => 20],
          [['mobile', 'home', 'work'], 'string', 'max' => 11]
          ];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-25
        • 1970-01-01
        • 2019-12-09
        • 2017-12-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多