dev-master
9999999-devThe enhanced restful api for Yii Framework 2.0
BSD
The Requires
by Harry Zheng
api rest yii2 restful
The enhanced restful api for Yii Framework 2.0
这个扩展主要是用于增强Yii2 rest api中IndexAction的功能., (*1)
相对于官方的IndexAction,该扩展主要增加了以下几个功能, (*2)
1.搜索功能, (*3)
http://url/users?id=1&username=LIKE_dmi&created_at=MAX_1398153715&addresses.city=南京
2.显示多级子资源(官方只支持二级子资源(通过expand关键字指定),如users?expand=address), (*4)
http://url/users?expand=addresses,friends.addresses&expand-fields=addresses.phone,friends.addresses
3.允许通过子资源排序, (*5)
http://url/users?sort=addresses.phone DESC,id ASC
4.允许为子资源的表设置别名(防止两个不同的子资源取同一张表时的命名冲突), (*6)
use harryzheng0907\rest\ActiveRecord; class User extends ActiveRecord { public static function tableName() { return 'user'; } public function getMainAddresses() { return $this->hasMany(Address::className(), ['user_id' => 'id'],'mainAddresses'); } public function getOtherAddresses() { return $this->hasMany(Address::className(), ['user_id' => 'id'],'otherAddresses'); } }
通过 composer安装, (*7)
php composer.phar require harryzheng0907/yii2-rest
使用方式其实就是两点, (*8)
1.新建一个全局父Controller,继承ActiveController,重新指定IndexAction, (*9)
namespace app\controllers; use yii\helpers\ArrayHelper; class ActiveController extends \yii\rest\ActiveController { public function actions() { return ArrayHelper::merge(parent::actions(),[ 'index' => [ 'class' => 'harryzheng0907\rest\IndexAction' ] ]); } }
2.新建特定资源的AR Model,继承harryzheng0907\rest\ActiveRecord,如user, (*10)
namespace app\models; use Yii; use harryzheng0907\rest\ActiveRecord; class User extends ActiveRecord { public static function tableName() { return 'user'; } }
3.新建特定资源的controller,如user,继承第一步新建的全局controller, (*11)
namespace app\controllers; class UserController extends \app\controllers\ActiveController { public $modelClass = 'app\models\User'; }
4.完成, (*12)
EQUAL:http://url/users?username=EQUAL_a // username = 'a' NOTEQUAL:http://url/users?username=NOTEQUAL_a // username != 'a' NULL:http://url/users?username=NULL_ // username IS NULL LIKE:http://url/users?username=LIKE_a //username LIKE '%a%' LLIKE:http://url/users?username=LLIKE_a //username LIKE '%a' RLIKE:http://url/users?username=RLIKE_a //username LIKE 'a%' IN:http://url/users?username=IN_a,b,c //username IN ('a','b','c') NOTIN:http://url/users?username=NOTIN_a,b,c //username NOT IN ('a','b','c') MIN:http://url/users?age_min=MIN_10 // age >= 10 MAX:http://url/users?age_max=Max_60 //age <= 60 RANGE:http://url/users?birthdate=RANGE_2015-03 //birthdate<=2015-03-31 AND birthdate >= 2015-03-01
use harryzheng0907\rest\ActiveRecord; class User extends ActiveRecord { public static function tableName() { return 'user'; } public function getMainAddresses() { return $this->hasMany(Address::className(), ['user_id' => 'id'],'mainAddresses'); } public function getOtherAddresses() { return $this->hasMany(Address::className(), ['user_id' => 'id'],'otherAddresses'); } }
2.设置排序时,DESC/ASC不可省略, (*13)
The enhanced restful api for Yii Framework 2.0
BSD
api rest yii2 restful