文档:http://www.yiichina.com/doc/guide/2.0/rest-quick-start
关于yii2的接口,使用起来是比较方便,配置起来也比较方便,不过按照模块和非模块的配置,在一些地方是不一样的
先配置一个普通的restful
我配置的是以模块的方式进行的配置:
1. 配置好模块
2. 定义controller ,继承于 yii\rest\ActiveController
<?php
namespace myapp\frontend\code\ECM\Customer\controllers;
use Yii;
use yii\rest\ActiveController;
class IndexController extends ActiveController
{
public $modelClass = 'myapp\frontend\code\ECM\User\models\Product';
}
3.创建资源:
数据库部分:
CREATE TABLE IF NOT EXISTS `restful_product` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`description` varchar(255) DEFAULT NULL,
`image` varchar(255) DEFAULT NULL,
`status` int(5) DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
--
-- 转存表中的数据 `restful_product`
--
INSERT INTO `restful_product` (`id`, `name`, `description`, `image`, `status`, `created_at`) VALUES
(1, 'terry', 'terry des', 'terry img', 1, '2015-11-17 15:25:21'),
(2, 'xxxx', 'ddddd', NULL, NULL, NULL),
(3, 'xxxx', 'ddddd', NULL, NULL, NULL),
(4, 'xxxx', 'ddddd', NULL, NULL, NULL),
(5, 'xxxx', 'ddddd', NULL, NULL, NULL);
创建完数据表 restful_product
剩下的工作就是创建model:
<?php
namespace myapp\frontend\code\ECM\User\models;
use yii\db\ActiveRecord;
class Product extends ActiveRecord
{
# 定义rule
public function rules(){
return [
[['name','description'],'required'],
];
}
public static function tableName()
{
return 'restful_product';
}
}
到这里,资源就建立好了
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:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
'' => 'cms/index',
['class' => 'yii\rest\UrlRule', 'controller' => 'customer/index',
'pluralize' => false,
],
//'POST customer' => 'customer/index/create',
],
],
'request' => [
'class' => '\yii\web\Request',
'enableCookieValidation' => false,
'parsers' => [
'application/json' => 'yii\web\JsonParser',
],
'cookieValidationKey' => 'O1d232trde1xww-M97_7QvwPo-5QGdkLMp#@#@',
],
在上面,需要配置request 组件 的 parsers子项,这个子项的意思是解析,也就是对于request请求,按照json格式,这样,发送post请求,通过接口插入数据,可以用json格式,而不是用繁琐的xml格式费劲。
对于urlManager组件 的rules子项,
['class' => 'yii\rest\UrlRule', 'controller' => 'customer/index',
'pluralize' => false,
],
class的值是系统默认的, controller的值是您的controller的路径,如果您不使用模块,直接在controller下面建立了一个CustomerController,那么controller的值是customer, 由于我使用的是customer模块,因此controller 是customer/index
pluralize的意思是不使用复数,这样可以不需要加s
5
测试:
get 方式可以直接使用浏览器访问
post请求:
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"}'
执行后,发现被保存到数据表中
官方给予的:
GET /users: 逐页列出所有用户
HEAD /users: 显示用户列表的概要信息
POST /users: 创建一个新用户
GET /users/123: 返回用户 123 的详细信息
HEAD /users/123: 显示用户 123 的概述信息
PATCH /users/123 and PUT /users/123: 更新用户123
DELETE /users/123: 删除用户123
OPTIONS /users: 显示关于末端 /users 支持的动词
OPTIONS /users/123: 显示有关末端 /users/123 支持的动词
对应的方法为:
index: list resources page by page;
view: return the details of a specified resource;
create: create a new resource;
update: update an existing resource;
delete: delete the specified resource;
options: return the supported HTTP methods.
[
'PUT,PATCH users/<id>' => 'user/update',
'DELETE users/<id>' => 'user/delete',
'GET,HEAD users/<id>' => 'user/view',
'POST users' => 'user/create',
'GET,HEAD users' => 'user/index',
'users/<id>' => 'user/options',
'users' => 'user/options',
]
为什么可以直接访问,因为在资源建立的时候,activerecord已经把上面的方法都默认建立好,如果您想更改对应的方法
可以在资源model里面新建一个对应的方法即可重写。