在文档指南中有一个示例:

    namespace app\controllers;

    use yii\rest\ActiveController;

    class UserController extends ActiveController
    {
        public $modelClass = 'app\models\User';
}


但是我不明白如何进行行动。

例如:


数据库具有具有多对多关系的表(通过Junction表)。
支持与模型一起使用,并根据传输的数据从多个表中形成一个共同的响应。可以返回一个数组或一个对象数组。


在命令控制器中使用它时,它就像:

    class LastTweetsController extends Controller
    {
        /**
         * @param int $count
         *
         * @throws yii\base\InvalidConfigException
         */
        public function actionIndex($count = 10)
        {
            /** @var TweetLastfinder $tweetLastFinder */
            $tweetLastFinder = Yii::$app->get('tweetlastfinder');

            /**
             * @var TweetShow $tweetShow
             */
            $tweetShow = Yii::$app->get('tweetshow');

            // For show tweets into terminal:
$tweetShow->showLastTweetsJSON($tweetLastFinder->findLastTweets($count));
        }
    }


但是我如何在ActiveController中进行相同的操作(传输参数$ count并以JSON返回结果)?

最佳答案

更改API的默认操作,例如-创建,更新,查看,索引,删除
在控制器中编写以下代码

namespace app\controllers;
use yii\rest\ActiveController;

class UserController extends ActiveController
{
    public $modelClass = 'app\models\User';

   /* Declare actions supported by APIs (Added in api/modules/v1/components/controller.php too) */
    public function actions(){
        $actions = parent::actions();
        unset($actions['create']);
        unset($actions['update']);
        unset($actions['delete']);
        unset($actions['view']);
        unset($actions['index']);
        return $actions;
    }

    /* Declare methods supported by APIs */
    protected function verbs(){
        return [
            'create' => ['POST'],
            'update' => ['PUT', 'PATCH','POST'],
            'delete' => ['DELETE'],
            'view' => ['GET'],
            'index'=>['GET'],
        ];
    }

    public function actionIndex($count = 10){
        /** @var TweetLastfinder $tweetLastFinder */
        $tweetLastFinder = Yii::$app->get('tweetlastfinder');

        /**
         * @var TweetShow $tweetShow
         */
        $tweetShow = Yii::$app->get('tweetshow');

        // This will return in JSON:
        return $tweetLastFinder->findLastTweets($count);
    }
}


在API主配置文件中-

    'components' => [
        ....
        'urlManager' => [
            'enablePrettyUrl' => true,
            'enableStrictParsing' => true,
            'showScriptName' => false,
            'rules' => [
                 [
                        'class' => 'yii\rest\UrlRule',
                        'controller' => 'v1/user',
                        'tokens' => [
                            '{id}' => '<id:\\w+>',
                            '{count}' => '<count:\\w+>',
                        ],
                        //'pluralize' => false,
                        'extraPatterns' => [
                            'POST' => 'create', // 'xxxxx' refers to 'actionXxxxx'
                            'PUT {id}' => 'update',
                            'PATCH {id}' => 'update',
                            'DELETE {id}' => 'delete',
                            'GET {id}' => 'view',
                            'GET {count}' => 'index',
                        ],

                    ],
            ]
        ],
        ....
    ]


在您的情况下,您需要在索引操作参数中使用$ count,因此在url管理器中,您需要像'id'一样定义'count'令牌

10-06 13:16
查看更多