This is my SiteController
这是我SiteController
public function actionIndex(){
$data['book']=[
['id'=>1]
['id'=>2]
['id'=>3]
];
return $this->render('index',$data);
}
public function actionView($id){
$data['id']=$id;
return $this->render('view',$data);
}
A simple index.php file
一个简单的指数。php文件
<?php foreach ($book as $bok){ $id =$bok['id']?>
<a href="index.php?r=site%2Fview?id=<?=$id?>">Views</a>
<?php } ?>
and view.php
和view.php
<?= $id?>
The problem I am having is however that I get an error 404 page not found on the view?id=1 or any other number. I know I have the view.php file in the right place and I do get an error if I try to go to view without an id (the page then tells me that it is missing required parameters: id). I don't know what is causing the problem nor how to solve it. Could you please help?
我遇到的问题是,我在视图上找不到一个错误404页面?id=1或其他任何数字。我知道我有这个想法。php文件在正确的位置,如果我试图在没有id的情况下进入view,就会出现错误(页面会告诉我缺少必需的参数:id)。我不知道是什么引起了这个问题,也不知道如何解决它。你能帮助吗?
1 个解决方案
#1
1
instead of a direct formatting of the url you could use urlHelper
可以使用urlHelper代替url的直接格式化
use yii\helpers\Url;
.....
<?php foreach ($book as $bok){
echo "<a href='" . Url::to(['site/view', 'id' => $bok['id'] ]."'>Views</a>";
}
?>
#1
1
instead of a direct formatting of the url you could use urlHelper
可以使用urlHelper代替url的直接格式化
use yii\helpers\Url;
.....
<?php foreach ($book as $bok){
echo "<a href='" . Url::to(['site/view', 'id' => $bok['id'] ]."'>Views</a>";
}
?>