IndexAction.php 1.55 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?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;

/**
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class IndexAction extends Action
{
19 20 21 22 23 24 25 26 27 28 29 30 31 32
    /**
     * @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;
Qiang Xue committed
33

34

35 36 37 38 39 40 41 42
    /**
     * @return ActiveDataProvider
     */
    public function run()
    {
        if ($this->checkAccess) {
            call_user_func($this->checkAccess, $this->id);
        }
Qiang Xue committed
43

44 45
        return $this->prepareDataProvider();
    }
Qiang Xue committed
46

47 48 49 50 51 52 53 54 55
    /**
     * 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);
        }
Qiang Xue committed
56

57
        /* @var $modelClass \yii\db\BaseActiveRecord */
58
        $modelClass = $this->modelClass;
Qiang Xue committed
59

60 61 62 63
        return new ActiveDataProvider([
            'query' => $modelClass::find(),
        ]);
    }
Qiang Xue committed
64
}