Php Yii2:ActiveController中的restapi操作
在文档指南中有以下示例:Php Yii2:ActiveController中的restapi操作,php,json,rest,yii2,Php,Json,Rest,Yii2,在文档指南中有以下示例: namespace app\controllers; use yii\rest\ActiveController; class UserController extends ActiveController { public $modelClass = 'app\models\User'; } 但我不明白,如何运用行动 例如: 数据库具有多对多关系的表(通过连接表) Сcomponent使用模型,并根据传输的数据从
namespace app\controllers;
use yii\rest\ActiveController;
class UserController extends ActiveController
{
public $modelClass = 'app\models\User';
}
但我不明白,如何运用行动
例如:
- 数据库具有多对多关系的表(通过连接表)
- Сcomponent使用模型,并根据传输的数据从多个表中形成共同响应。可以返回一个数组或一个对象数组
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中执行相同的操作(以JSON格式传输参数$count并返回结果)?以更改API的默认操作,如-create、update、view、index、delete 在控制器中编写以下代码
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',
],
],
]
],
....
]
“组件”=>[
....
“urlManager”=>[
“enablePrettyUrl”=>true,
'enableStrictParsing'=>true,
'showScriptName'=>false,
“规则”=>[
[
'class'=>'yii\rest\UrlRule',
“控制器”=>“v1/用户”,
“代币”=>[
“{id}”=>”,
“{count}”=>”,
],
//“多元化”=>错误,
“外部模式”=>[
“POST”=>“create”/“xxxxx”指的是“actionXxxxx”
'PUT{id}'=>'update',
'补丁{id}'=>'更新',
'DELETE{id}'=>'DELETE',
'GET{id}'=>'view',
'GET{count}'=>'index',
],
],
]
],
....
]
在您的情况下,您需要在索引操作参数中使用$count,因此在url管理器中,您需要像'id'一样定义'count'标记。谢谢,在我结束问题之前,请您稍微解释一下这段代码。还有我如何传输paramaeter$count和actionIndex返回数组,它是在组件中准备的…而不是json。json的回应是正确的。我的错,我找到了答案)TKSI非常感谢。我仍然对如何在默认操作中使用组件响应有疑问,但我已经在另一个问题中问过了)