簡體   English   中英

Yii2:ActiveController中的REST API操作

[英]Yii2: REST API actions in ActiveController

在文檔指南中有例子:

    namespace app\controllers;

    use yii\rest\ActiveController;

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

但我不明白,如何處理行動。

例如:

  • DataBase具有多對多關系的表(通過Junction表)。

  • Сomponent根據傳輸的數據處理模型並從多個表中形成共同響應。 可以返回數組或對象數組。

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

    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 in index action參數,所以在url manager你需要定義'count'標記,就像'id'一樣

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM