博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
yii2 RESTful 接口 api -6: 写一个自己的api
阅读量:4200 次
发布时间:2019-05-26

本文共 3571 字,大约阅读时间需要 11 分钟。

1. 创建api的 应用分区,类似backend   frontend   这样可以用一个单独的域名,以及单独的配置

可以复制frontend过来,然后改改

然后在\common\config\bootstrap.php中加入

Yii::setAlias('api', dirname(dirname(__DIR__)) . '/api');

1.配置:

'request' => [			'class' => '\yii\web\Request',            'enableCookieValidation' => false,            'parsers' => [                'application/json' => 'yii\web\JsonParser',            ],			'cookieValidationKey' => 'O1d232trde1xww-M97_7QvwPo-5QGdkLMp#@#@',		],'urlManager' =>  [			'class' => 'yii\web\UrlManager',			'enablePrettyUrl' => true,			'enableStrictParsing' => true,			'showScriptName' => false,			'rules' => [				'' => 'site/index',				['class' => 'yii\rest\UrlRule', 'controller' => 'customer/api',				  'pluralize' => false,				],				#  定义方法: public function actionSearch($id);   
就是search方法传入的参数 # http://10.10.10.252:599/v1/wishorder/2015-08-11+12:12:12/2015-09-11+12:12:12?access-token=pBJi3hyFsLsTuvUM9paFpWjYRatn3qwS # 分页:http://10.10.10.252:599/v1/wishorder/2015-08-11+12:12:12/2015-09-11+12:12:12?page=2&access-token=pBJi3hyFsLsTuvUM9paFpWjYRatn3qwS 'GET v1/wishorder/
/
' => 'v1/wishorder/viewbydate', 'GET v1/ebayorder/
/
' => 'v1/ebayorder/viewbydate', ], ];

 

2.myapp\code\core\Api\V1\controllers\WishorderController;

CompositeAuth::className(), 'authMethods' => [ # 下面是三种验证access_token方式 //HttpBasicAuth::className(), //HttpBearerAuth::className(), # 这是GET参数验证的方式 # http://10.10.10.252:600/user/index/index?access-token=xxxxxxxxxxxxxxxxxxxx QueryParamAuth::className(), ], ]; #定义返回格式是:JSON $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON; return $behaviors; } public function actionViewbydate($begin_datetime,$end_datetime){ # 分页参数处理 $numPerPage = Request::param('numPerPage'); if(!$numPerPage || $numPerPage <1 || $numPerPage > 800 ){ $numPerPage = 100; }else{ $numPerPage = (int)$numPerPage; } # where条件处理 $where = ''; if($begin_datetime){ $where .= " last_updated >= '".$begin_datetime."' "; } if($end_datetime){ if($where){ $where .= " AND last_updated < '".$end_datetime."' "; }else{ $where .= " last_updated < '".$end_datetime."' "; } } //echo $where;exit; if(!$where){ throw new \yii\web\HttpException(404, 'You Must Add Where Filter By DateTime'); } $query = WishOrder::find()->where($where); if($query->count() <1){ throw new \yii\web\HttpException(404, 'No entries found with this query string'); } $provider = new ActiveDataProvider([ 'query' => $query, 'pagination' => [ 'pageSize' => $numPerPage, ], ]); return $provider; }}

3.myapp\code\core\Api\V1\models\wish\WishOrder

wishDb; } public static function tableName() { return 'wish_order'; } public function fields(){ return [ 'id', 'platform_order_id', 'platform_order_states', 'wish_name', 'transaction_id', 'sku', 'qty', 'adapter', 'spu', 'spu_atrribute', 'order_total', 'spu_price', 'spu_shipping', 'spu_name', 'sku_china_name', 'spu_image_url', 'last_updated', 'days_to_fulfill', 'hours_to_fulfill', 'order_create_time', 'customer_name', 'customer_country', 'customer_province', 'customer_city', 'customer_street_address1', 'customer_street_address2', 'customer_zipcode', 'customer_phone', 'customer_note', 'shipping_provider', 'tracking_number', 'warehouse_id', 'real_ship_cost', 'real_ship_cost_currency', ]; }}?>
关于账户验证和速度控制参看:

http://blog.csdn.net/terry_water/article/details/49903507

你可能感兴趣的文章
论文浅尝 | 机器阅读理解中常识知识的显式利用
查看>>
论文浅尝 | 通过共享表示和结构化预测进行事件和事件时序关系的联合抽取
查看>>
论文浅尝 | 融合多粒度信息和外部语言知识的中文关系抽取
查看>>
论文浅尝 | GMNN: Graph Markov Neural Networks
查看>>
应用实践 | 网络智能运维下的知识图谱
查看>>
应用实践 | 南方科技大学研发基于新型冠状病毒知识图谱模式挖掘系统
查看>>
征稿 | 2020年全国知识图谱与语义计算大会
查看>>
论文浅尝 | Tree-to-sequence 学习知识问答
查看>>
论文浅尝 | 重新实验评估知识图谱补全方法
查看>>
论文浅尝 | GEOM-GCN: Geometric Graph Convolutional Networks
查看>>
论文浅尝 | ICLR 2020 - 图神经网络的预训练策略
查看>>
开源开放 | 《大词林》开源 75 万核心实体和围绕核心实体的细粒度概念、关系列表...
查看>>
论文浅尝 | AAAI2020 - 基于生成对抗的知识图谱零样本关系学习
查看>>
应用实践 | 电商应用——一种基于强化学习的特定规则学习模型
查看>>
论文浅尝 - ACL2020 | 用于链接预测的开放知识图谱嵌入
查看>>
陈华钧等 | OpenKG区块链:构建可信开放的联邦知识图谱平台
查看>>
论文浅尝 | Iterative Cross-Lingual Entity Alignment Based on TransC
查看>>
论文浅尝 – KDD2020 | 使用图对比编码的图神经网络预训练模型
查看>>
论文浅尝 - CIKM2020 | Relation Reflection Entity Alignment
查看>>
论文浅尝 | 基于对抗学习的弱监督知识图谱对齐
查看>>