Codeigniter 将数据从控制器传递到视图 [英] Codeigniter passing data from controller to view
问题描述
根据这里我有以下控制器:
class User extends CI_Controller {
public function Login()
{
//$data->RedirectUrl = $this->input->get_post('ReturnTo');
$data = array(
'title' => 'My Title',
'heading' => 'My Heading',
'message' => 'My Message'
);
$this->load->view('User_Login', $data);
}
//More...
}
在我的 User_Login.php
视图文件中,我这样做:
and in my User_Login.php
view file I do this:
<?php print_r($data);?>
导致:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: views/User_Login.php
Line Number: 1
我是否需要加载任何特定的模块/帮助程序来填充 $data 变量?如果我 print_r($this)
,我可以看到很多东西,但除了缓存中没有我的数据
Do I need to load any specific modules/helpers to get the $data variable populated? If I print_r($this)
, I can see a lot of stuff but none of my data except in caches
澄清一下,我知道在控制器和视图中调用相同的变量不会共享"它 - 它超出了范围但在我链接的示例中,它似乎暗示了 $data
变量是在视图范围内创建的.我只是碰巧在控制器中使用了相同的名称
To clarify, I know that calling the variable the same in the controller and view won't "share" it - it's out of scope but in the example I linked, it seems to imply a $data
variable is created in the scope of the view. I simply happened to use the same name in the controller
推荐答案
啊,$data
数组的键被转换成变量:try var_dump($title);
例如.
Ah, the $data
array's keys are converted into variables: try var_dump($title);
for example.
这是使用 extract 完成的.
这篇关于Codeigniter 将数据从控制器传递到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!