(yii2) 404 error page not found

This is my 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 foreach ($book as $bok){ $id =$bok['id']?>
        <a href="index.php?r=site%2Fview?id=<?=$id?>">Views</a>
<?php } ?>

      

and view.php

<?= $id?>

      

However, the problem is I got a 404 error of a page not found in the view? id = 1 or any other number. I know I have a view.php file in the right place and I get an error if I try to navigate to a view without an id (the page then tells me that it is missing the required parameters: id). I have no idea what is causing the problem and how to solve it. Could you help me?

+3


source to share


1 answer


instead of directly formatting the url you can use urlHelper



  use yii\helpers\Url;

 .....

<?php foreach ($book as $bok){ 
        echo "<a href='" . Url::to(['site/view', 'id' => $bok['id'] ]."'>Views</a>";
  } 
?>

      

+1


source







All Articles