2017-02-28 129 views
0

我试图获得所有帖子下的帖子#{any nos.},但我坚持如何获得post_id当我点击下面的URI。例如交#1在嵌套资源中获取父ID

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(); 
} 

回答

0

化妆用_urlManager的yii2

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

    ] 
]; 
+0

我使用同一种网址的'后台/ company/id/details',这个文件驻留在'/ backend/config/_urlManager.php'中....让我知道它是否适合你 –