yii2 RESTful 接口 api -1 : 接口的基本配置

原文:http://www.fancyecommerce.com/2016/05/04/yii2-restful-接口-api-1-:-接口的基本配置/

关于yii2的接口,使用起来是比较方便,配置起来也比较方便,不过按照模块和非模块的配置,在一些地方是不一样的,对于restful api的哲学思想,可以搜索资料,总体来说restfulapi是通过请求类型的不同来决定具体的操作,简略来说,restful api并不是一门技术,而是一种规则,实际当中,很少有人严格的按照restful的哲学思想设计API,一般都是用post类型,好了,下面说下restful api的知识,如何在yii2中使用。

先配置一个普通的restful

我配置的是以模块的方式进行的配置:

1. 配置好模块

2. 定义controller   ,继承于 yii\rest\ActiveController

  1. <?php
  2. namespace myapp\frontend\code\ECM\Customer\controllers;
  3. use Yii;
  4. use yii\rest\ActiveController;
  5. class IndexController extends ActiveController
  6. {
  7. public $modelClass = 'myapp\frontend\code\ECM\User\models\Product';
  8. }

3.创建资源:

数据库部分:

  1. CREATE TABLE IF NOT EXISTS `restful_product` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `name` varchar(255) DEFAULT NULL,
  4. `description` varchar(255) DEFAULT NULL,
  5. `image` varchar(255) DEFAULT NULL,
  6. `status` int(5) DEFAULT NULL,
  7. `created_at` datetime DEFAULT NULL,
  8. PRIMARY KEY (`id`)
  9. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
  10. --
  11. -- 转存表中的数据 `restful_product`
  12. --
  13. INSERT INTO `restful_product` (`id`, `name`, `description`, `image`, `status`, `created_at`) VALUES
  14. (1, 'terry', 'terry des', 'terry img', 1, '2015-11-17 15:25:21'),
  15. (2, 'xxxx', 'ddddd', NULL, NULL, NULL),
  16. (3, 'xxxx', 'ddddd', NULL, NULL, NULL),
  17. (4, 'xxxx', 'ddddd', NULL, NULL, NULL),
  18. (5, 'xxxx', 'ddddd', NULL, NULL, NULL);

创建完数据表  restful_product

剩下的工作就是创建model:

  1. <?php
  2. namespace myapp\frontend\code\ECM\User\models;
  3. use yii\db\ActiveRecord;
  4. class Product extends ActiveRecord
  5. {
  6. # 定义rule
  7. public function rules(){
  8. return [
  9. [['name','description'],'required'],
  10. ];
  11. }
  12. public static function tableName()
  13. {
  14. return 'restful_product';
  15. }
  16. }

到这里,资源就建立好了

4.进行配置

在配置的时候,一定要注意,在下面的文件都可以配置

frontend/config/main.php  ,  frontend/config/main-local.php

common/config/main.php , common/config/main-local.php

不要重复,曾经,我配置mailer 的时候,就是因为在两个配置文件配置了mailer组件,导致我的配置没有生效,搞了我半天晕头转向,最后发现是重复配置,

在哲学里面,灵活的东西,对应的就是使用的复杂性,这是无可避免,所以,没有最好,只有更适合,yii2是比较适合架构师玩,定制好规则和example,然后让码农们

填写代码,如果是初学者,玩玩thinkphp,如果玩yii2,则不是很适合,什么都有一个进化的过程(http://blog.csdn.net/terry_water),所以没有最好的框架,只有更适合自己的现状,和公司现状的框架,找女人也是这样吧。

下面要配置的是urlManage:

  1. 'urlManager' => [
  2. 'class' => 'yii\web\UrlManager',
  3. 'enablePrettyUrl' => true,
  4. 'enableStrictParsing' => true,
  5. 'showScriptName' => false,
  6. 'rules' => [
  7. '' => 'cms/index',
  8. ['class' => 'yii\rest\UrlRule', 'controller' => 'customer/index',
  9. 'pluralize' => false,
  10. ],
  11. //'POST customer' => 'customer/index/create',
  12. ],
  13. ],
  14. 'request' => [
  15. 'class' => '\yii\web\Request',
  16. 'enableCookieValidation' => false,
  17. 'parsers' => [
  18. 'application/json' => 'yii\web\JsonParser',
  19. ],
  20. 'cookieValidationKey' => 'O1d232trde1xww-M97_7QvwPo-5QGdkLMp#@#@',
  21. ],

 

在上面,需要配置request  组件    的 parsers子项,这个子项的意思是解析,也就是对于request请求,按照json格式,这样,发送post请求,通过接口插入数据,可以用json格式,而不是用繁琐的xml格式费劲。

对于urlManager组件  的rules子项,

  1. ['class' => 'yii\rest\UrlRule', 'controller' => 'customer/index',
  2. 'pluralize' => false,
  3. ],

class的值是系统默认的, controller的值是您的controller的路径,如果您不使用模块,直接在controller下面建立了一个CustomerController,那么controller的值是customer, 由于我使用的是customer模块,因此controller 是customer/index

pluralize默认是true,即

访问会把控制器名称变为复数

设置为false的意思是不使用复数,这样可以不需要加s

5

测试:

get 方式可以直接使用浏览器访问

post请求:

  1. curl -i -H "Accept:application/json" -H "Content-Type:application/json" -XPOST "http://10.10.10.252:800/index.php/customer/index" -d '{"name": "xxxx", "description":"ddddd"}'

官方给予的:

  1. GET /users: 逐页列出所有用户
  2. HEAD /users: 显示用户列表的概要信息
  3. POST /users: 创建一个新用户
  4. GET /users/123: 返回用户 123 的详细信息
  5. HEAD /users/123: 显示用户 123 的概述信息
  6. PATCH /users/123 and PUT /users/123: 更新用户123
  7. DELETE /users/123: 删除用户123
  8. OPTIONS /users: 显示关于末端 /users 支持的动词
  9. OPTIONS /users/123: 显示有关末端 /users/123 支持的动词

对应的方法为:

  1. index: list resources page by page;
  2. view: return the details of a specified resource;
  3. create: create a new resource;
  4. update: update an existing resource;
  5. delete: delete the specified resource;
  6. options: return the supported HTTP methods.

 

  1. [
  2. 'PUT,PATCH users/<id>' => 'user/update',
  3. 'DELETE users/<id>' => 'user/delete',
  4. 'GET,HEAD users/<id>' => 'user/view',
  5. 'POST users' => 'user/create',
  6. 'GET,HEAD users' => 'user/index',
  7. 'users/<id>' => 'user/options',
  8. 'users' => 'user/options',
  9. ]

 

为什么可以直接访问,因为在资源建立的时候,activerecord已经把上面的方法都默认建立好,如果您想更改对应的方法

可以在资源model里面新建一个对应的方法即可重写。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值