在嵌套资源中获取父ID

时间:2017-02-28 17:20:31

标签: php web-services rest yii2

我试图在帖子#{any nos.}下收到所有评论,但是当我点击下面的URI时,我怎么能得到post_id。例如帖子#1

关于GET URI:/posts/1/comments/

CommentController上的

public function actions() {
   $actions = parent::actions();
   unset($actions['index']);
   return $actions;
}

public function actionIndex($post_id)
{
  return Comments::find()->where(['post_id' => $post_id])->all();
}

1 个答案:

答案 0 :(得分:0)

在yii2中使用_urlManager

return [
   'class'=>'yii\web\UrlManager',
   'enablePrettyUrl'=>true,
   'showScriptName'=>false,
   'rules'=>[
      // url rules
      'posts/<post_id:\d+>/comments' => 'posts/index',

     ]
 ];