状态404未找到:yii2 RESTful API PUT方法

I'm following this link to build a set of RESTful APIs: http://www.yiiframework.com/doc-2.0/guide-rest-quick-start.html

First, create a controller as follows,

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

Then, modify the configuration about the urlManager component in my application configuration:

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

Trying it Out

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

works fine, can get json output.

But when I: PUT http://e9049d9.tunnel.mobi/yii2-basic/web/index.php?r=duangorder/restorder/1 it's said Status 404 Not Found

Where is the problem? Thanks...

updated:

now I think should add update in 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"}

but processed's still not be updated...

doutou3725
doutou3725 我也试过使用extendsActiveController,结果是一样的。
大约 5 年之前 回复
drphfy1198
drphfy1198 在您提供的链接中,Controller类扩展了ActiveController而不是AbstractRestController。例如。:classUserController扩展ActiveController
大约 5 年之前 回复
dsiimyoc804955
dsiimyoc804955 对不起,刚才我的服务被关闭了。现在它再次打开。请重试。
大约 5 年之前 回复
douyou9923
douyou9923 第一个链接给我“找不到隧道17517303.tunnel.mobi”
大约 5 年之前 回复

2个回答

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"

I also got the same problem. In config/main.php set the accepted tokens in the urlManager, in case of GET or PUT the {id}.

This is my solution:

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

Hope this helps

Refer these links for more info about Yii2 API:

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐