PHP code example of sndwow / yii2-rest-query-helper
1. Go to this page and download the library: Download sndwow/yii2-rest-query-helper library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sndwow / yii2-rest-query-helper example snippets
public function actionIndex()
{
...
$rules = [
'where' => [
'id' => '*', // 允许任意查询条件
'name' => ['eq'], // 只允许等于查询
'books.name'=>['like','in'] // 允许like查询和in查询
],
// 只允许以下字段排序
'sort' => [
'id',
'books.id'
]
];
...
}
'sndwow/yii2-rest-query-helper' =>
array (
'name' => 'sndwow/yii2-rest-query-helper',
'version' => '1.0.1.0',
'alias' =>
array (
'@sndwow/rest' => $vendorDir . '/sndwow/yii2-rest-query-helper',
),
),
class UserController extends \yii\rest\Controller
{
public $serializer = 'sndwow\rest\Serializer';
public function actionIndex()
{
// 仅在此方法指定
// $this->serializer = 'sndwow\rest\Serializer';
// 使用User作为主表,也可以使用类名:new QueryHelper('app\models\User')
$helper = new QueryHelper(User::className())
// 设置查询、排序规则
$rules = [
'where' => [
'id' => '*', // 允许任意查询条件
'name' => ['eq'], // 只允许等于查询
'books.name'=>['like','in'] // 允许like查询和in查询
],
// 只允许以下字段排序
'sort' => [
'id',
'books.id'
]
];
// 应用规则,并返回query实例
// 实例与普通的query一样,只不过关联了相关数据,例如可以 $query->asArray()->all()
$query = $helper->build($rules);
// 使用此方式返回可以被sndwow\rest\Serializer进行处理
// 同时也支持yii model 里的 fields,可自定义返回字段及数据
return new ActiveDataProvider([
'query' => $query
]);
}
}
namespace app\modules\v1\controllers;
use app\modules\v1\models\Category;
use app\modules;
use sndwow\rest\QueryHelper;
use yii\data\ActiveDataProvider;
use yii\rest\ActiveController;
class CategoryController extends ActiveController
{
public $modelClass = 'app\modules\v1\models\Category';
public function actions()
{
$actions = parent::actions();
unset($actions['index']);
return $actions;
}
public function actionIndex()
{
$this->serializer = 'sndwow\rest\Serializer';
$rules = [
'sort' => [
'id',
'create_time',
'items.markets.update_time'
],
'where' => [
'id' => '*',
'name' => ['like', 'eq'],
'items.name' => ['like'],
]
];
$helper = new QueryHelper(Category::className());
$query = $helper->build($rules);
return new ActiveDataProvider([
'query' => $query
]);
}
}
namespace app\modules\v1\controllers;
use app\modules\v1\models\Category;
use app\modules;
use sndwow\rest\QueryHelper;
use yii\data\ActiveDataProvider;
use yii\rest\Controller;
class CategoryController extends Controller
{
public $modelClass = 'app\modules\v1\models\Category';
public function actionIndex()
{
$this->serializer = 'sndwow\rest\Serializer';
$rules = [
'sort' => [
'id',
'create_time',
'items.markets.update_time'
],
'where' => [
'id' => '*',
'name' => ['like', 'eq'],
'items.name' => ['like'],
]
];
$helper = new QueryHelper(Category::className());
$query = $helper->build($rules);
return new ActiveDataProvider([
'query' => $query
]);
}
}
// user表id使用asc排序(不穿排序方式默认asc),books.name使用倒序,books.author.age倒序
api.com/users?id=1,books.name=like:自然&_sort=id,books.name.desc,books.author.age.desc
// 将返回关联的books,以及books内关联的author数据
api.com/users?_expand=books,books.author