RESTFullYii
RESTFull API for your Yii application
Install / Use
/learn @evan108108/RESTFullYiiREADME
Starship / RestfullYii
Makes quickly adding a RESTFul API to your Yii project easy. RestfullYii provides full HTTP verb support (GET, PUT, POST, DELETE) for your resources, as well as the ability to offset, limit, sort, filter, etc… . You will also have the ability to read and manipulate related data with ease.
RestfullYii has been lovingly rebuilt from the metal and is now 100% test covered! The new event based architecture allows for clean and unlimited customization.
How it works
RestfullYii adds a new set of RESTFul routes to your standard routes, but prepends '/api' .
So if you apply RestfullYii to the 'WorkController' you will get the following new routes by default.
[GET] http://yoursite.com/api/work (returns all works)
[GET] http://yoursite.com/api/work/1 (returns work with PK=1)
[POST] http://yoursite.com/api/work (create new work)
[PUT] http://yoursite.com/api/work/1 (update work with PK=1)
[DELETE] http://yoursite.com/api/work/1 (delete work with PK=1)
Requirements
- PHP 5.4.0 (or later)*
- YiiFramework 1.1.14 (or later)
- PHPUnit 3.7 (or later) to run tests.
For older versions of PHP (< 5.4) checkout v1.15 or cccssw's amazing 5.3 port: https://github.com/cccssw/RESTFullYii
Installation
Installing Manually
-
Download and place the 'starship' directory in your Yii extension directory.
-
In config/main.php you will need to add the RestfullYii alias. This allows for flexability in where you place the extension.
'aliases' => array(
.. .
'RestfullYii' =>realpath(__DIR__ . '/../extensions/starship/RestfullYii'),
.. .
),
- Include ext.starship.RestfullYii.config.routes in your main config (see below) or copy the routes and paste them in your components->urlManager->rules in same config.
'components' => array(
'urlManager' => array(
'urlFormat' => 'path',
'rules' => require(
dirname(__FILE__).'/../extensions/starship/RestfullYii/config/routes.php'
),
),
)
Installing With Composer
{
"require": {
"starship/restfullyii": "dev-master"
}
}
- In config/main.php you will need to add the RestfullYii alias. This allows for flexability in where you place the extension.
'aliases' => array(
.. .
//Path to your Composer vendor dir plus starship/restfullyii path
'RestfullYii' =>realpath(__DIR__ . '/../../../vendor/starship/restfullyii/starship/RestfullYii'),
.. .
),
- Include ext.starship.RestfullYii.config.routes in your main config (see below) or copy the routes and paste them in your components->urlManager->rules in same config.
'components' => array(
'urlManager' => array(
'urlFormat' => 'path',
'rules' => require(
dirname(__FILE__).'/../../../vendor/starship/restfullyii/starship/RestfullYii/config/routes.php
),
),
)
##Controller Setup Adding a set of RESTFul actions to a controller.
- Add the ERestFilter to your controllers filter method.
public function filters()
{
return array(
'accessControl', // perform access control for CRUD operations
array(
'RestfullYii.filters.ERestFilter +
REST.GET, REST.PUT, REST.POST, REST.DELETE, REST.OPTIONS'
),
);
}
- Add the ERestActionProvider to your controllers actions method.
public function actions()
{
return array(
'REST.'=>'RestfullYii.actions.ERestActionProvider',
);
}
- If you are using the accessControl filter you need to make sure that access is allowed on all RESTFul routes.
public function accessRules()
{
return array(
array('allow', 'actions'=>array('REST.GET', 'REST.PUT', 'REST.POST', 'REST.DELETE', 'REST.OPTIONS'),
'users'=>array('*'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
Making Requests
To understand how to make RestfullYii API requests its best to look at a few examples. Code examples will be shown first in JavaScript* as an AJAX user* and then using CURL.
* JS examples use jQuery
* Default validation for an AJAX user is !Yii::app()->user->isGuest so the user must be logged in for this type of request.
###GET Requests
Getting a list or resources (WorkController)
JavaScript:
$.ajax({
url:'/api/work',
type:"GET",
success:function(data) {
console.log(data);
},
error:function (xhr, ajaxOptions, thrownError){
console.log(xhr.responseText);
}
});
CURL:
curl -i -H "Accept: application/json" -H "X-REST-USERNAME: admin@restuser" -H "X-REST-PASSWORD: admin@Access"\
http://my-site.com/api/work
Response:
{
"success":true,
"message":"Record(s) Found",
"data":{
"totalCount":"30",
"work":[
{
"id": "1",
"title": "title1",
"author_id": "1",
"content": "content1",
"create_time": "2013-08-07 10:09:41"
},
{
"id": "2",
"title": "title2",
"author_id": "2",
"content": "content2",
"create_time": "2013-08-08 11:01:11"
},
. . .,
]
}
}
Getting a single resource (WorkController)
JavaScript:
$.ajax({
url:'/api/work/1',
type:"GET",
success:function(data) {
console.log(data);
},
error:function (xhr, ajaxOptions, thrownError){
console.log(xhr.responseText);
}
});
CURL:
curl -i -H "Accept: application/json" -H "X-REST-USERNAME: admin@restuser" -H "X-REST-PASSWORD: admin@Access"\
http://my-site.com/api/work/1
Response:
{
"success":true,
"message":"Record Found",
"data":{
"totalCount":"1",
"work":[
{
"id": "1",
"title": "title1",
"author_id": "1",
"content": "content1",
"create_time": "2013-08-07 10:09:41"
}
]
}
}
GET Request: Limit & Offset (WorkController)
You can limit and paginate through your results by adding the limit and offset variables to the request query string.
JavaScript:
$.ajax({
url:'/api/work?limit=10&offset=30',
type:"GET",
success:function(data) {
console.log(data);
},
error:function (xhr, ajaxOptions, thrownError){
console.log(xhr.responseText);
}
});
CURL:
curl -i -H "Accept: application/json" -H "X-REST-USERNAME: admin@restuser" -H "X-REST-PASSWORD: admin@Access"\
http://my-site.com/api/work?limit=10&offset=30
Response:
{
"success":true,
"message":"Record(s) Found",
"data":{
"totalCount":"30",
"work":[
{
"id": "11",
"title": "title11",
"author_id": "11",
"content": "content11",
"create_time": "2013-08-11 11:10:09"
},
{
"id": "12",
"title": "title12",
"author_id": "12",
"content": "content12",
"create_time": "2013-08-08 12:11:10"
},
. . .,
]
}
}
GET Request: Sorting results (WorkController)
You can sort your results by any valid param or multiple params as well as provide a sort direction (ASC or DESC). sort=[{"property":"title", "direction":"DESC"}, {"property":"create_time", "direction":"ASC"}]
JavaScript:
$.ajax({
url:'/api/work?sort=[{"property":"title", "direction":"DESC"}, {"property":"create_time", "direction":"ASC"}]',
type:"GET",
success:function(data) {
console.log(data);
},
error:function (xhr, ajaxOptions, thrownError){
console.log(xhr.responseText);
}
});
CURL:
curl -i -H "Accept: application/json" -H "X-REST-USERNAME: admin@restuser" -H "X-REST-PASSWORD: admin@Access"\
http://my-site.com/api/work?sort=%5B%7B%22property%22%3A%22title%22%2C+%22direction%22%3A%22DESC%22%7D%2C+%7B%22property%22%3A%22create_time%22%2C+%22direction%22%3A%22ASC%22%7D%5D
Response:
{
"success":true,
"message":"Record(s) Found",
"data":{
"totalCount":"30",
"work":[
{
"id": "29",
"title": "title30b",
"author_id": "29",
"content": "content30b",
"create_time": "2013-08-07 14:05:01"
},
{
"id": "30",
"title": "title30",
"author_id": "30",
"content": "content30",
"create_time": "2013-08-08 09:10:09"
},
{
"id": "28",
"title": "title28",
"author_id": "28",
"content": "content28",
"create_time": "2013-08-09 14:05:01"
},
. . .,
]
}
}
GET Request: Filtering results (WorkController)
You can filter your results by any valid param or multiple params as well as an operator.
Available filter operators:
- in
- not in
- =
- !=
-
-
=
- <
- <=
- No operator is "LIKE"
/api/post/?filter = [
{"property": "id", "value" : 50, "operator": ">="}
, {"property": "user_id", "value" : [1, 5, 10, 14], "operator": "in"}
, {"property": "state", "value" : ["save", "deleted"], "operator": "not in"}
, {"property": "date", "value" : "2013-01-01", "operator": ">="}
, {"property": "date", "value" : "2013-01-31", "operator": "<="}
, {"property": "type", "value" : 2, "operator": "!="}
]
###POST Requests (Creating new resources) With POST requests we must include the resource data as a JSON object in the request body.
JavaScript:
var postData = {
"title": "title31",
"author_id": "31",
"content": "content31",
"create_time": "2013-08-20 09:23:14"
};
$.ajax({
url:'/api/work',
data:JSON.stringify(postData)
type:"POST",
success:function(data) {
console.log(data);
},
error:function (xhr, ajaxOptions, thrownError){
console.log(xhr.responseText);
}
});
CURL:
curl -l -H "Accept: application/json" -H "X-REST-USERNAME: admin@restuser" -H "X-REST-PASSWORD: admin@Access"\
-X POST -
Related Skills
node-connect
347.9kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
108.7kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.9kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.9kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
