YII2.0 & RESTful Web 服务

发布:2017-06-09 12:55   点击470次   评论:0

=================================

http://blog.csdn.net/huangxy10/article/details/17849557
当然深入Restful的理论还是发现比较复杂的,但是,开发和理论并不需要那么的贴合,有时候伪Restful更直观,靠谱些。

=================================

http://www.yiichina.com/code/611
动态AR,不用每个表都建model啦 (未实践测试)

业务中有时候有这种情况出现,有些数据表是动态生成的,这时候如果有一个动态AR可以使用,这真是极好的。
在网上搜了一圈,只有github上yii2的issue里,
有人提供了一些思路,我就沿着这个思路进行了一些完善,代码如下:

================================================================

https://stackoverflow.com/questions/40468731/yii2-post-method-in-not-allowed
Yii2 post method in not allowed

按文:RESTful Web 服务 - 快速入门(Quick Start)
按文档建立ApiController 后;
POST出现:Method Not Allowed[405]
This url can only handle the following request methods: GET, HEAD.

... 找了好多方法,就这个找对了... 

Try write something like this in your TestController:

protected function verbs() {
$verbs = parent::verbs();
$verbs['index'] => ['POST']; //methods you need in action
//or
$verbs['index'][] => 'POST'; //just add the 'POST' to "GET" and "HEAD"
return $verbs;
}

================================================================

http://www.yiichina.com/tutorial/633
YII2.0的RESTFUL开发-返回结果为JSON的修改方法

修改:vender/yiisoft/yii2/rest/ActiveController.php 文件
引入类:use yii\web\Response;
增加属性:protected $formatType = 'json';

重写方法:

public function behaviors()
{
$behaviors = parent::behaviors();
switch($this->formatType)
{
default :
case 'json' :
case 'jsonp' :
$formatType = Response::FORMAT_JSON;
$behaviors['contentNegotiator']['formats'] = [];
$behaviors['contentNegotiator']['formats']['application/json'] = $formatType;
break;
case 'xml' :
$formatType = Response::FORMAT_XML;
$behaviors['contentNegotiator']['formats'] = [];
$behaviors['contentNegotiator']['formats']['application/xml'] = $formatType;
break;            
case 'html' :
$formatType = Response::FORMAT_HTML;
$behaviors['contentNegotiator']['formats'] = [];
$behaviors['contentNegotiator']['formats']['html/text'] = $formatType;
break;
}
return $behaviors;
}

评论(0条)   >>发布 评论列表

关于 GitHub 导航 部门 反馈

提示:`/chn.php`入口数据仅为演示功能,不构成任何交易凭证,也不承担相关风险和责任!

Copyright © 2011-2018 xxxxx.com All rights reserved. 粤ICP备07020797号.

Done:9.137/41.721(ms); 6(queries)/2.225(MB); Tpl:comm:news/detail; Upd:2019-08-20 09:50:53