Controller.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. /**
  40. * {@inheritdoc}
  41. */
  42. public function behaviors()
  43. {
  44. return [
  45. 'contentNegotiator' => [
  46. 'class' => ContentNegotiator::className(),
  47. 'formats' => [
  48. 'application/json' => Response::FORMAT_JSON,
  49. 'application/xml' => Response::FORMAT_XML,
  50. ],
  51. ],
  52. 'verbFilter' => [
  53. 'class' => VerbFilter::className(),
  54. 'actions' => $this->verbs(),
  55. ],
  56. 'authenticator' => [
  57. 'class' => CompositeAuth::className(),
  58. ],
  59. 'rateLimiter' => [
  60. 'class' => RateLimiter::className(),
  61. ],
  62. ];
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function afterAction($action, $result)
  68. {
  69. $result = parent::afterAction($action, $result);
  70. return $this->serializeData($result);
  71. }
  72. /**
  73. * Declares the allowed HTTP verbs.
  74. * Please refer to [[VerbFilter::actions]] on how to declare the allowed verbs.
  75. * @return array the allowed HTTP verbs.
  76. */
  77. protected function verbs()
  78. {
  79. return [];
  80. }
  81. /**
  82. * Serializes the specified data.
  83. * The default implementation will create a serializer based on the configuration given by [[serializer]].
  84. * It then uses the serializer to serialize the given data.
  85. * @param mixed $data the data to be serialized
  86. * @return mixed the serialized data.
  87. */
  88. protected function serializeData($data)
  89. {
  90. return Yii::createObject($this->serializer)->serialize($data);
  91. }
  92. }