当有 1 个结果时从搜索中呈现详细视图页面 yii2

Render Detail view page yii2 from search when there is 1 result

在yii2中,如果搜索结果只有1条,如何自动进入详情页

谢谢。

$dataprovider 之后的控制器中:

$count=$dataProvider->getTotalCount();

if($count==1){

$dataProvider->getModels();
// get id and redirect view
//redirect to view

}else{

//redirect to gridview or listview

}

我认为这将是 yii2 的基本用法..

     $searchModel = new PostSearch();   // Search Model can be created by gii 
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        $count=    $dataProvider->getTotalCount();

              if($count==1){

                    $a =   $dataProvider->getModels();
                     $id = $a[0]['id'] ;    // accessing 2nd level array elements   or $dataProvider->getModels()[0]['id'];

                     return $this->render('view', [
                         'model' => $this->findModel($id), //findModel($id) is protected function in the controller
                     ]);

              }