Controller.php 2.8 KB

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