No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

100 líneas
2.7KB

  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\web\Response;
  13. use yii\filters\VerbFilter;
  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. * @author Qiang Xue <qiang.xue@gmail.com>
  26. * @since 2.0
  27. */
  28. class Controller extends \yii\web\Controller
  29. {
  30. /**
  31. * @var string|array the configuration for creating the serializer that formats the response data.
  32. */
  33. public $serializer = 'yii\rest\Serializer';
  34. /**
  35. * @inheritdoc
  36. */
  37. public $enableCsrfValidation = false;
  38. /**
  39. * @inheritdoc
  40. */
  41. public function behaviors()
  42. {
  43. return [
  44. 'contentNegotiator' => [
  45. 'class' => ContentNegotiator::className(),
  46. 'formats' => [
  47. 'application/json' => Response::FORMAT_JSON,
  48. 'application/xml' => Response::FORMAT_XML,
  49. ],
  50. ],
  51. 'verbFilter' => [
  52. 'class' => VerbFilter::className(),
  53. 'actions' => $this->verbs(),
  54. ],
  55. 'authenticator' => [
  56. 'class' => CompositeAuth::className(),
  57. ],
  58. 'rateLimiter' => [
  59. 'class' => RateLimiter::className(),
  60. ],
  61. ];
  62. }
  63. /**
  64. * @inheritdoc
  65. */
  66. public function afterAction($action, $result)
  67. {
  68. $result = parent::afterAction($action, $result);
  69. return $this->serializeData($result);
  70. }
  71. /**
  72. * Declares the allowed HTTP verbs.
  73. * Please refer to [[VerbFilter::actions]] on how to declare the allowed verbs.
  74. * @return array the allowed HTTP verbs.
  75. */
  76. protected function verbs()
  77. {
  78. return [];
  79. }
  80. /**
  81. * Serializes the specified data.
  82. * The default implementation will create a serializer based on the configuration given by [[serializer]].
  83. * It then uses the serializer to serialize the given data.
  84. * @param mixed $data the data to be serialized
  85. * @return mixed the serialized data.
  86. */
  87. protected function serializeData($data)
  88. {
  89. return Yii::createObject($this->serializer)->serialize($data);
  90. }
  91. }