3

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?>

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?

STepHan
  • 57
  • 1
  • 11

1 Answers1

1

instead of a direct formatting of the url you could use urlHelper

  use yii\helpers\Url;

 .....

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