Codeigniter将数据从控制器传递到视图 [英] Codeigniter passing data from controller to view
问题描述
根据此处,我有以下控制器:
As per here I've got the following controller:
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
我需要加载任何特定的模块/ helpers来获取$ 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.
:这是使用解压缩完成的。
这篇关于Codeigniter将数据从控制器传递到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!