【问题标题】:Status 404 Not Found: yii2 RESTful APIs PUT method未找到状态 404:yii2 RESTful APIs PUT 方法
【发布时间】:2015-12-10 01:20:27
【问题描述】:

我正在关注此链接以构建一组 RESTful API: http://www.yiiframework.com/doc-2.0/guide-rest-quick-start.html

首先,如下创建一个控制器,

class RestorderController extends AbstractRestController {
    public $modelClass = 'app\modules\duangorder\models\Order';
}

然后,修改我的应用配置中关于urlManager组件的配置:

'urlManager' => [
    'class' => 'yii\web\UrlManager',
    'enablePrettyUrl' => true,
    'enableStrictParsing' => true,
    'showScriptName' => false,
    'rules' => [
        ['class' => 'yii\rest\UrlRule', 'controller' => 'duangorder/restorder'],
    ],
],

试一试

获取http://e9049d9.tunnel.mobi/yii2-basic/web/index.php?r=duangorder/restorder&token=gh_9831478f66cb

工作正常,可以得到json输出。

但是当我: PUT http://e9049d9.tunnel.mobi/yii2-basic/web/index.php?r=duangorder/restorder/1 据说找不到状态 404

问题出在哪里?谢谢...

更新:

现在我认为应该在 url 中添加更新:

curl -l -H "Accept: application/json" -H '"processed":"0"' -X PUT -d '{"processed":"0"}' "http://e9049d9.tunnel.mobi/yii2-basic/web/index.php?r=duangorder/restorder/update&id=1"

{"id":1,"token":"gh_9831478f66cb","order_id":"143978240336383","openid":"oPuB9wkVKTXn9AZ9idM-XqhUvxh0","table_id":0,"ctime":1439782403,"phone":"13211675827","address":"address","remark":"quick","detail":"{\"4\":{\"num\":2,\"id\":\"4\",\"name\":\"\\u86cb\\u7092\\u996d\",\"price\":\"10.00\",\"total\":20},\"6\":{\"num\":1,\"id\":\"6\",\"name\":\"\\u626c\\u5dde\\u7092\\u996d\",\"price\":\"10.00\",\"total\":\"10.00\"},\"5\":{\"num\":1,\"id\":\"5\",\"name\":\"\\u51b0\\u6dc7\\u51cc\",\"price\":\"8.00\",\"total\":\"8.00\"}}","name":"tingjun","processed":"1","paid":"0"}

但是处理的还没有更新...

【问题讨论】:

  • 第一个链接给了我“Tunnel 17517303.tunnel.mobi not found”
  • @Glapa,抱歉,刚才我的服务被关闭了。现在又开了。请重试。
  • 在您提供的链接中,Controller 类扩展 ActiveController 而不是 AbstractRestController。例如。 :class UserController extends ActiveController
  • 我也试过extends ActiveController,结果是一样的。

标签: php yii2


【解决方案1】:
curl -l  -X PUT -d processed=1 "http://e9049d9.tunnel.mobi/yii2-basic/web/index.php?r=duangorder/restorder/update&id=1"

curl -l  -X PUT -d processed=0 "http://e9049d9.tunnel.mobi/yii2-basic/web/index.php?r=duangorder/restorder/update&id=1"

【讨论】:

    【解决方案2】:

    我也遇到了同样的问题。在 config/main.php 中,在 urlManager 中设置接受的令牌,以防 GET 或 PUT {id}。

    这是我的解决方案:

            'urlManager' => [
            'enablePrettyUrl' => true,
            'enableStrictParsing' => true,
            'showScriptName' => false,
            'rules' => [
                [
                    'class' => 'yii\rest\UrlRule',
                    'controller' => 'v1/country'   // our country api rule,
                    'tokens' => [
                        '{id}' => '<id:\\w+>'
                    ]
                ]
            ],
        ]
    

    希望对你有帮助

    有关 Yii2 API 的更多信息,请参阅这些链接:

    http://budiirawan.com/setup-restful-api-yii2/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-18
      • 2016-04-22
      • 2019-07-30
      相关资源
      最近更新 更多