Isaac Li
Isaac Li

Reputation: 155

Status 404 Not Found: yii2 RESTful APIs PUT method

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' => 'yii\rest\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...

Upvotes: 0

Views: 943

Answers (2)

Daniele Montes
Daniele Montes

Reputation: 126

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' => 'yii\rest\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/

Upvotes: 0

Isaac Li
Isaac Li

Reputation: 155

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"

Upvotes: 0

Related Questions