123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <?php
- /**
- * @link http://www.yiiframework.com/
- * @copyright Copyright (c) 2008 Yii Software LLC
- * @license http://www.yiiframework.com/license/
- */
- namespace yii\rest;
- use Yii;
- use yii\data\ActiveDataProvider;
- /**
- * IndexAction implements the API endpoint for listing multiple models.
- *
- * For more details and usage information on IndexAction, see the [guide article on rest controllers](guide:rest-controllers).
- *
- * @author Qiang Xue <qiang.xue@gmail.com>
- * @since 2.0
- */
- class IndexAction extends Action
- {
- /**
- * @var callable a PHP callable that will be called to prepare a data provider that
- * should return a collection of the models. If not set, [[prepareDataProvider()]] will be used instead.
- * The signature of the callable should be:
- *
- * ```php
- * function ($action) {
- * // $action is the action object currently running
- * }
- * ```
- *
- * The callable should return an instance of [[ActiveDataProvider]].
- */
- public $prepareDataProvider;
- /**
- * @return ActiveDataProvider
- */
- public function run()
- {
- if ($this->checkAccess) {
- call_user_func($this->checkAccess, $this->id);
- }
- return $this->prepareDataProvider();
- }
- /**
- * Prepares the data provider that should return the requested collection of the models.
- * @return ActiveDataProvider
- */
- protected function prepareDataProvider()
- {
- if ($this->prepareDataProvider !== null) {
- return call_user_func($this->prepareDataProvider, $this);
- }
- /* @var $modelClass \yii\db\BaseActiveRecord */
- $modelClass = $this->modelClass;
- return Yii::createObject([
- 'class' => ActiveDataProvider::className(),
- 'query' => $modelClass::find(),
- ]);
- }
- }
|