Rosti Rosti - 17 days ago 11
PHP Question

Create view action in gridview

How to make just view action in my gridview,I already have list of my data and view button, but I want to see different record when click that view button. Now I see only error and my url change for id of that records like /index/0 or /index/1.

<?php
use yii\grid\GridView;
?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'ID'=>'id',
'Title'=>'post_title',
'Text'=>'post_text',
'Author'=>'name',
[
'class' => 'yii\grid\ActionColumn',
'header'=>'Действия',
'headerOptions' => ['width' => '80'],
'template' => '{view}',
],
],
'rowOptions' => function ($model, $key, $index, $grid) {
return ['id' => $model['id']];
},
]); ?>


I have that error:

An Error occurred while handling another error:
exception 'yii\base\InvalidRouteException' with message 'Unable to resolve the request: site/error' in W:\XAMPP\htdocs\test\basic\vendor\yiisoft\yii2\base\Controller.php:125
Stack trace:
#0 W:\XAMPP\htdocs\test\basic\vendor\yiisoft\yii2\base\Module.php(454): yii\base\Controller->runAction('error', Array)
#1 W:\XAMPP\htdocs\test\basic\vendor\yiisoft\yii2\web\ErrorHandler.php(93): yii\base\Module->runAction('site/error')
#2 W:\XAMPP\htdocs\test\basic\vendor\yiisoft\yii2\base\ErrorHandler.php(109): yii\web\ErrorHandler->renderException(Object(yii\web\NotFoundHttpException))
#3 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\NotFoundHttpException))
#4 {main}
Previous exception:
exception 'yii\base\InvalidRouteException' with message 'Unable to resolve the request: site/view' in W:\XAMPP\htdocs\test\basic\vendor\yiisoft\yii2\base\Controller.php:125
Stack trace:
#0 W:\XAMPP\htdocs\test\basic\vendor\yiisoft\yii2\base\Module.php(454): yii\base\Controller->runAction('view', Array)
#1 W:\XAMPP\htdocs\test\basic\vendor\yiisoft\yii2\web\Application.php(100): yii\base\Module->runAction('site/view', Array)
#2 W:\XAMPP\htdocs\test\basic\vendor\yiisoft\yii2\base\Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))
#3 W:\XAMPP\htdocs\test\basic\index.php(12): yii\base\Application->run()
#4 {main}

Next exception 'yii\web\NotFoundHttpException' with message 'Page not found.' in W:\XAMPP\htdocs\test\basic\vendor\yiisoft\yii2\web\Application.php:112
Stack trace:
#0 W:\XAMPP\htdocs\test\basic\vendor\yiisoft\yii2\base\Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))
#1 W:\XAMPP\htdocs\test\basic\index.php(12): yii\base\Application->run()
#2 {main}

Answer

Seems you don't have a view name view.php in your site/view

be sure you have a viewAction in your site Controller

class SiteController extends Controller
{
   ....
    // this is only a sample
   public function actionView($id)
   {
       return $this->render('view', [
        'model' => $this->findModel($id),
       ]);
    }
    ...
}

And a view.php in your yourAppName/views/site/ directory

Comments