You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IndexAction.php 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. /**
  11. * @author Qiang Xue <qiang.xue@gmail.com>
  12. * @since 2.0
  13. */
  14. class IndexAction extends Action
  15. {
  16. /**
  17. * @var callable a PHP callable that will be called to prepare a data provider that
  18. * should return a collection of the models. If not set, [[prepareDataProvider()]] will be used instead.
  19. * The signature of the callable should be:
  20. *
  21. * ```php
  22. * function ($action) {
  23. * // $action is the action object currently running
  24. * }
  25. * ```
  26. *
  27. * The callable should return an instance of [[ActiveDataProvider]].
  28. */
  29. public $prepareDataProvider;
  30. /**
  31. * @return ActiveDataProvider
  32. */
  33. public function run()
  34. {
  35. if ($this->checkAccess) {
  36. call_user_func($this->checkAccess, $this->id);
  37. }
  38. return $this->prepareDataProvider();
  39. }
  40. /**
  41. * Prepares the data provider that should return the requested collection of the models.
  42. * @return ActiveDataProvider
  43. */
  44. protected function prepareDataProvider()
  45. {
  46. if ($this->prepareDataProvider !== null) {
  47. return call_user_func($this->prepareDataProvider, $this);
  48. }
  49. /* @var $modelClass \yii\db\BaseActiveRecord */
  50. $modelClass = $this->modelClass;
  51. return new ActiveDataProvider([
  52. 'query' => $modelClass::find(),
  53. ]);
  54. }
  55. }