Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Yii2 rest API获取用户发布_Php_Rest_Yii2 - Fatal编程技术网

Php Yii2 rest API获取用户发布

Php Yii2 rest API获取用户发布,php,rest,yii2,Php,Rest,Yii2,我正在YII2中创建RESTAPI。我通过调用getAPI获取所有post数据 /post/ /post/1/ 但我想让用户也发布那个特定的帖子 例如,我想要以下格式的数据 { "id":"1", "title":"kapil", "content" : "test", "user" : { "username":"admin", "first_name":"kapil", "last_nam

我正在
YII2
中创建RESTAPI。我通过调用getAPI获取所有post数据

/post/
/post/1/
但我想让用户也发布那个特定的帖子

例如,我想要以下格式的数据

{
      "id":"1",
      "title":"kapil",
      "content" : "test",
      "user" : {
         "username":"admin",
         "first_name":"kapil",
         "last_name":"sharma",
          //blah blah
      }

}
但反应是

{
          "id":"1",
          "title":"kapil",
          "content" : "test",
}

我使用教程创建API。

假设在post方法中,您有
getIdUser()
关系:

public function getIdUser() {
  return $this->hasOne(User::className(), ['id' => 'user_id']);
}
在该模型中,您应该使用
extraFields()
方法,如下所示:

public function extraFields() {
  return [
    'user' => 'idUser' // or the name you hasOne relation with user has
  ];
}
然后,使用
expand
参数调用REST API,在这里指定您希望在本例中包括的
外部字段
详细信息:


您的代码在哪里?我只创建了控制器,其余的工作默认情况下都完成了Yi2 rest API格式如何创建关系。我是新来的