状态404未找到:yii2 RESTful API中PUT方法
问题描述:
我以下链接构建一套基于REST的API: http://www.yiiframework.com/doc-2.0/guide-rest-quick-start.html状态404未找到:yii2 RESTful API中PUT方法
首先,创建一个控制器,如下所示,
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'],
],
],
想出来
GET 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"}
,但处理的还是不更新......
答
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"
答
我也有同样的问题。在config/main.php中,在GETManager或PUT {id}的情况下,在urlManager中设置接受的令牌。
这是我的解决方案:
'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的更多信息:
首先链接给我“隧道17517303.tunnel .mobi找不到“ – Glapa
@Glapa,抱歉,刚才我的服务被关闭了。现在它再次打开。请重试。 –
在您提供的链接中,Controller类扩展'ActiveController'而不是'AbstractRestController'。例如。 :'类UserController扩展ActiveController' – Glapa