1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\rest; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\filters\auth\CompositeAuth; |
12
|
|
|
use yii\filters\ContentNegotiator; |
13
|
|
|
use yii\filters\RateLimiter; |
14
|
|
|
use yii\web\Response; |
15
|
|
|
use yii\filters\VerbFilter; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Controller is the base class for RESTful API controller classes. |
19
|
|
|
* |
20
|
|
|
* Controller implements the following steps in a RESTful API request handling cycle: |
21
|
|
|
* |
22
|
|
|
* 1. Resolving response format (see [[ContentNegotiator]]); |
23
|
|
|
* 2. Validating request method (see [[verbs()]]). |
24
|
|
|
* 3. Authenticating user (see [[\yii\filters\auth\AuthInterface]]); |
25
|
|
|
* 4. Rate limiting (see [[RateLimiter]]); |
26
|
|
|
* 5. Formatting response data (see [[serializeData()]]). |
27
|
|
|
* |
28
|
|
|
* For more details and usage information on Controller, see the [guide article on rest controllers](guide:rest-controllers). |
29
|
|
|
* |
30
|
|
|
* @author Qiang Xue <[email protected]> |
31
|
|
|
* @since 2.0 |
32
|
|
|
*/ |
33
|
|
|
class Controller extends \yii\web\Controller |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var string|array the configuration for creating the serializer that formats the response data. |
37
|
|
|
*/ |
38
|
|
|
public $serializer = 'yii\rest\Serializer'; |
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
|
|
public $enableCsrfValidation = false; |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
*/ |
48
|
|
|
public function behaviors() |
49
|
|
|
{ |
50
|
|
|
return [ |
51
|
|
|
'contentNegotiator' => [ |
52
|
|
|
'class' => ContentNegotiator::className(), |
53
|
|
|
'formats' => [ |
54
|
|
|
'application/json' => Response::FORMAT_JSON, |
55
|
|
|
'application/xml' => Response::FORMAT_XML, |
56
|
|
|
], |
57
|
|
|
], |
58
|
|
|
'verbFilter' => [ |
59
|
|
|
'class' => VerbFilter::className(), |
60
|
|
|
'actions' => $this->verbs(), |
61
|
|
|
], |
62
|
|
|
'authenticator' => [ |
63
|
|
|
'class' => CompositeAuth::className(), |
64
|
|
|
], |
65
|
|
|
'rateLimiter' => [ |
66
|
|
|
'class' => RateLimiter::className(), |
67
|
|
|
], |
68
|
|
|
]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @inheritdoc |
73
|
|
|
*/ |
74
|
23 |
|
public function afterAction($action, $result) |
75
|
|
|
{ |
76
|
23 |
|
$result = parent::afterAction($action, $result); |
77
|
23 |
|
return $this->serializeData($result); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Declares the allowed HTTP verbs. |
82
|
|
|
* Please refer to [[VerbFilter::actions]] on how to declare the allowed verbs. |
83
|
|
|
* @return array the allowed HTTP verbs. |
84
|
|
|
*/ |
85
|
|
|
protected function verbs() |
86
|
|
|
{ |
87
|
|
|
return []; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Serializes the specified data. |
92
|
|
|
* The default implementation will create a serializer based on the configuration given by [[serializer]]. |
93
|
|
|
* It then uses the serializer to serialize the given data. |
94
|
|
|
* @param mixed $data the data to be serialized |
95
|
|
|
* @return mixed the serialized data. |
96
|
|
|
*/ |
97
|
23 |
|
protected function serializeData($data) |
98
|
|
|
{ |
99
|
23 |
|
return Yii::createObject($this->serializer)->serialize($data); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|