IndexAction.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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\data\ActiveDataProvider;
  10. use yii\data\DataFilter;
  11. /**
  12. * IndexAction implements the API endpoint for listing multiple models.
  13. *
  14. * For more details and usage information on IndexAction, see the [guide article on rest controllers](guide:rest-controllers).
  15. *
  16. * @author Qiang Xue <qiang.xue@gmail.com>
  17. * @since 2.0
  18. */
  19. class IndexAction extends Action
  20. {
  21. /**
  22. * @var callable a PHP callable that will be called to prepare a data provider that
  23. * should return a collection of the models. If not set, [[prepareDataProvider()]] will be used instead.
  24. * The signature of the callable should be:
  25. *
  26. * ```php
  27. * function (IndexAction $action) {
  28. * // $action is the action object currently running
  29. * }
  30. * ```
  31. *
  32. * The callable should return an instance of [[ActiveDataProvider]].
  33. *
  34. * If [[dataFilter]] is set the result of [[DataFilter::build()]] will be passed to the callable as a second parameter.
  35. * In this case the signature of the callable should be the following:
  36. *
  37. * ```php
  38. * function (IndexAction $action, mixed $filter) {
  39. * // $action is the action object currently running
  40. * // $filter the built filter condition
  41. * }
  42. * ```
  43. */
  44. public $prepareDataProvider;
  45. /**
  46. * @var DataFilter|null data filter to be used for the search filter composition.
  47. * You must setup this field explicitly in order to enable filter processing.
  48. * For example:
  49. *
  50. * ```php
  51. * [
  52. * 'class' => 'yii\data\ActiveDataFilter',
  53. * 'searchModel' => function () {
  54. * return (new \yii\base\DynamicModel(['id' => null, 'name' => null, 'price' => null]))
  55. * ->addRule('id', 'integer')
  56. * ->addRule('name', 'trim')
  57. * ->addRule('name', 'string')
  58. * ->addRule('price', 'number');
  59. * },
  60. * ]
  61. * ```
  62. *
  63. * @see DataFilter
  64. *
  65. * @since 2.0.13
  66. */
  67. public $dataFilter;
  68. /**
  69. * @return ActiveDataProvider
  70. */
  71. public function run()
  72. {
  73. if ($this->checkAccess) {
  74. call_user_func($this->checkAccess, $this->id);
  75. }
  76. return $this->prepareDataProvider();
  77. }
  78. /**
  79. * Prepares the data provider that should return the requested collection of the models.
  80. * @return ActiveDataProvider
  81. */
  82. protected function prepareDataProvider()
  83. {
  84. $requestParams = Yii::$app->getRequest()->getBodyParams();
  85. if (empty($requestParams)) {
  86. $requestParams = Yii::$app->getRequest()->getQueryParams();
  87. }
  88. $filter = null;
  89. if ($this->dataFilter !== null) {
  90. $this->dataFilter = Yii::createObject($this->dataFilter);
  91. if ($this->dataFilter->load($requestParams)) {
  92. $filter = $this->dataFilter->build();
  93. if ($filter === false) {
  94. return $this->dataFilter;
  95. }
  96. }
  97. }
  98. if ($this->prepareDataProvider !== null) {
  99. return call_user_func($this->prepareDataProvider, $this, $filter);
  100. }
  101. /* @var $modelClass \yii\db\BaseActiveRecord */
  102. $modelClass = $this->modelClass;
  103. $query = $modelClass::find();
  104. if (!empty($filter)) {
  105. $query->andWhere($filter);
  106. }
  107. return Yii::createObject([
  108. 'class' => ActiveDataProvider::className(),
  109. 'query' => $query,
  110. 'pagination' => [
  111. 'params' => $requestParams,
  112. ],
  113. 'sort' => [
  114. 'params' => $requestParams,
  115. ],
  116. ]);
  117. }
  118. }