我是Yii2的新手,如果这是一个简单的问题,请原谅。在这个url http://localhost/index.php/host
下,它显示索引。此外,当url类似于http://localhost/index.php/host/index
时,它会显示相同的内容。
当我单击Gridview
中的row
时,问题就出现了。这是我的index.php。
<?=
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
'id',
'name',
'hostgroup_id',
'ip_address' => [
'label' => 'IP',
'attribute' => 'ip_address'
],
'private_address' => [
'label' => 'Private IP',
'attribute' => 'private_address'
],
'object_name',
['class' => 'yii'grid'ActionColumn',
'template' => '{delete}',
],
],
'rowOptions' => function($model, $key, $index, $grid) {
$var = Yii::$app;
return [
'id' => $model['id'],
'onclick' => 'window.location.href=''update/'.'''+(this.id);',
];
}
]); ?>
当我在url http://localhost/index.php/host/index
中并单击链接时,我会重定向到http://cms.dev/index.php/host/update/1
但当我在http://localhost/index.php/host/
下时,我会重定向到http://cms.dev/index.php/update/1
我认为我的onclick
和rowOptions
的值不正确。有什么建议吗?
您也可以使用Url::to()来实现:
'rowOptions' => function($model) {
$url = Url::to(['controller/action', 'id' => $model['id']]);
return [
'onclick' => "window.location.href='{$url}'"
];
}
如果你正在使用当前控制器/视图,那么你可以像这个例子一样使用
例如
$url = Url::to([Yii::$app->controller->id.'/view', 'id' => $model['id']]);
In不清楚,因为不要设置localhost,但您可以尝试以这种方式使用url助手
在顶部添加对urlHelper的引用
use yii'helpers'Url;
return [
'id' => $model['id'],
'onclick' => "window.location.href='" .
Url::to(['update' , 'id' => $model['id']) "'",
];
(这是相对路径,但如果需要,您可以指定一个与应用程序相关的绝对路径。)