-
Notifications
You must be signed in to change notification settings - Fork 76
/
PostController.php
46 lines (40 loc) · 1.39 KB
/
PostController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
namespace rest\versions\v1\controllers;
use common\models\Post;
use yii\data\ActiveDataProvider;
use yii\filters\auth\QueryParamAuth;
use yii\rest\ActiveController;
class PostController extends ActiveController
{
public $modelClass = 'common\models\Post';
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => QueryParamAuth::className(),
];
return $behaviors;
}
public function actions()
{
return array_merge(
parent::actions(),
[
'index' => [
'class' => 'yii\rest\IndexAction',
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
'prepareDataProvider' => function ($action) {
/* @var $model Post */
$model = new $this->modelClass;
$query = $model::find();
$dataProvider = new ActiveDataProvider(['query' => $query]);
$model->setAttribute('title', @$_GET['title']);
$query->andFilterWhere(['like', 'title', $model->title]);
return $dataProvider;
}
]
]
);
}
}