我是Yii框架的新手。我正在尝试将数组传递给控制器。在我的视图页面
<?php
foreach ($simplified_list as $data) {
echo "<tr>
<td>" . CHtml::link($data['name'], array('view','data_list'=>$data)) . "</td>
//<td>" . $data['data_volume'] . "</td>
//<td>" . $data['tariff'] . "</td>
//<td>" . $data['tariff_with_vat'] . "</td>
</tr>";
}
?>
我使用 CHtml :: link($ data [&#39; name&#39;],数组(&#39; view&#39;,&#39; data_list&#39; =&gt; $ data) ))将 $ simplified_list 数组列表传递给我的控制器。这是我的$ simplified_list。
Array ( [success] => 1 [data] => Array ( [0] => Array ( [name] => 4MB 1Day [data_volume] => 4 MB [tariff] => 2 [tariff_with_vat] => 2.3 ) [1] => Array ( [name] => 25MB 1Day [data_volume] => 25 MB [tariff] => 10 [tariff_with_vat] => 11.5 ) [2] => Array ( [name] => 100MB 7Day [data_volume] => 100 MB [tariff] => 40 [tariff_with_vat] => 46 ) [3] => Array ( [name] => 500MB 30Days [data_volume] => 500 MB [tariff] => 175 [tariff_with_vat] => 210.25 ) [4] => Array ( [name] => 1GB 30Days [data_volume] => 1024 MB [tariff] => 275 [tariff_with_vat] => 316.25 ) [5] => Array ( [name] => 2GB 30Days [data_volume] => 2048 MB [tariff] => 347 [tariff_with_vat] => 399 ) ) )
在我的控制器中,这就是行动。
public function actionView($data_list) {
$model = new User;
$this->render('view', array(
'model' => $model, 'data_list' => $data_list));
}
当我点击链接时,它会呈现给视图页面。在我看来,我需要访问name,data_volume,tariff和tariff_with_vat记录。我在视图中使用了以下内容。
<?php echo $data_list['name'];?>
当我尝试渲染到视图页面时,这是错误。
Bad Request
Your request is invalid.
The request could not be understood by the server due to malformed syntax. Please do not repeat the request without modifications.
If you think this is a server error, please contact the webmaster.
我该如何解决这个问题。任何建议将不胜感激。
提前谢谢
答案 0 :(得分:1)
CHtml::link()
中的第二个参数是url,你传递数组。正确的语法是:
CHtml::link($data['name'], $this->createUrl('view',array('data_list'=>$data));
修改强> 从行动中移除参数:
public function actionView() {
$data_list = $_GET['data_list'];
$model = new User;
$this->render('view', array(
'model' => $model, 'data_list' => $data_list));
}
我记得,要在动作参数中传递变量,必须先将它们添加到网址规则中。