2017-02-28 136 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'中....讓我知道它是否適合你 –