goodnickoff / yiirestmodel
Yii RESTful API
dev-master
2017-10-06 11:41 UTC
Requires
- php: >=5.4.0
- yiisoft/yii: >=1.1.15
This package is not auto-updated.
Last update: 2024-09-14 16:50:09 UTC
README
安装
在protected/extensions下解压文件。
添加到配置
array('api/<controller>/list', 'pattern'=>'api/<controller:\w+>', 'verb'=>'GET'),
array('api/<controller>/view', 'pattern'=>'api/<controller:\w+>/<id:\d+>', 'verb'=>'GET'),
array('api/<controller>/create', 'pattern'=>'api/<controller:\w+>', 'verb'=>'POST'),
array('api/<controller>/update', 'pattern'=>'api/<controller:\w+>/<id:\d+>', 'verb'=>'PUT'),
array('api/<controller>/update', 'pattern'=>'api/<controller:\w+>', 'verb'=>'PUT'),
array('api/<controller>/delete', 'pattern'=>'api/<controller:\w+>/<id:\d+>', 'verb'=>'DELETE'),
array('api/<controller>/delete', 'pattern'=>'api/<controller:\w+>', 'verb'=>'DELETE'),
文档
用法
创建模块及其控制器。
控制器示例
class UsersController extends ApiController { public $safeAttributes = array( 'id', 'first_name', 'middle_name', 'last_name', 'email', ); public function __construct($id, $module = null) { $this->model = new User('read'); parent::__construct($id, $module); } /** * Function returns user data * @method GET */ public function actionView() { if (!Yii::app()->user->checkAccess('getUser')) { $this->accessDenied(); } $this->getView(); } /** * Function returns user list * @method GET */ public function actionList() { if (!Yii::app()->user->checkAccess('getUser')) { $this->accessDenied(); } $this->getList(); } /** * Function creates new user * @method POST */ public function actionCreate() { if (!Yii::app()->user->checkAccess('createUser')) { $this->accessDenied(); } $this->model->setScenario('create'); $this->create(); } /** * Function updates user. * @method PUT */ public function actionUpdate() { if (!Yii::app()->user->checkAccess('updateUser')) { $this->accessDenied(); } $this->model->setScenario('update'); $this->update(); } /** * Function deletes user. * @method DELETE */ public function actionDelete() { if (!Yii::app()->user->checkAccess('deleteUser')) { $this->accessDenied(); } $this->model->setScenario('delete'); $this->delete(); } public function getRelations() { return array( 'comments'=>array( // relation GET parameter name (...?with=comments) 'relationName'=>'comments', //model relation name 'columnName'=>'comments', //column name in response 'return'=>'array' //return array of arrays or array of models ) ); } }
获取记录
GET: /user - all users
GET: /user/2 - user with id=42
搜索和过滤
{"name":"alex", "age":"25"} — WHERE name='alex' AND age=25
[{"name":"alex"}, {"age":"25"}] WHERE name='alex' OR age=25
比较运算符基于给定值的前几个字符智能确定。特别地,如果以下运算符作为给定值的前导字符出现,则它将识别以下运算符
- <: 列表必须小于给定值。
-
: 列表必须大于给定值。
- <=: 列表必须小于或等于给定值。
-
=: 列表必须大于或等于给定值。
- <>: 列表必须不等于给定值。
- =: 列表必须等于给定值。
示例
GET: /users?filter={"name":"alex"} — user with name alex
GET: /users?filter={"name":"alex", "age":">25"} — user with name alex AND age greater than 25
GET: /users?filter=[{"name":"alex"}, {"name":"dmitry"}] — user with name alex OR dmitry
GET: /users?search={"name":"alex"} — user with name contains the substring alex (alexey, alexander, alex)
关系
GET: /user/1?with=comments,posts — get user data with comments and posts array (comma separated list of relations in `with` GET parameter)
{
"id":"1",
"first_name":"Alex",
"comments":[{"id":"1","text":"..."}, {"id":"2","text":"..."}],
"posts":[{"id":"1","content":"..."}, {"id":"2","content":"..."}],
...
}
删除
DELETE: /user/42 - delete user with id = 42
DELETE: /user - delete all users
DELETE: /user?filter={"first_name":"Alex"} - delete users with name 'Alex'
创建
POST: /user - create new user
创建集合
POST: /user - create new users
传递POST参数
[
{"name":"admin"},
{"name":"guest"}
]
创建两个用户 'admin' 和 'guest'
更新
PUT: /user/42 - update user with id = 42
更新集合
PUT: /user
传递POST参数
[
{"id":"1","name":"admin"},
{"id":"2","name":"guest"}
]
更新ID为1和2的用户
限制、偏移、排序
GET: /users/?offset=10&limit=10
GET: /users/?order=id DESC
GET: /users/?order=id ASC
GET: /users/?order=parent_id ASC,ordering ASC
GET: /users/?order=comment.id&with=comment
响应格式
默认情况下,以JSON格式发送响应。要更改响应格式,请通过传递值xml
的GET参数format
GET: /users?format=xml