Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将变量从MY_控制器传递到视图_Php_Codeigniter_Variables_Constructor - Fatal编程技术网

Php 将变量从MY_控制器传递到视图

Php 将变量从MY_控制器传递到视图,php,codeigniter,variables,constructor,Php,Codeigniter,Variables,Constructor,在我的网站上,我有一个“登录提示”,可以在每个页面上看到。我使用的是一个模板系统,所以这个登录提示出现在每个页面的标题中 当用户登录时,应显示其用户名和注销链接。当他们未登录时,将显示登录或注册的链接 我的_控制器中有一个功能,用于检查用户是否在每次加载页面时登录,该功能正常: if($this->is_logged_in()) { $this->username = $this->session->userdata('username'); $data[

在我的网站上,我有一个“登录提示”,可以在每个页面上看到。我使用的是一个模板系统,所以这个登录提示出现在每个页面的标题中

当用户登录时,应显示其用户名和注销链接。当他们未登录时,将显示登录或注册的链接

我的_控制器中有一个功能,用于检查用户是否在每次加载页面时登录,该功能正常:

if($this->is_logged_in()) {
    $this->username = $this->session->userdata('username');
    $data['login_prompt'] = "Hi, " . $this->username . " " . anchor("account/logout", "Logout");
}
在my header.php(视图)中,我有:

我希望
$data['login\u prompt']
在控制器的每个方法中都可用,以便可以将其传递到视图。但是,打印
$data['login\u prompt']
时会出现
'undefined index'
错误,因此header.php中定义的默认“登录或注册”消息始终可见

ucp.php中的典型方法如下:

function index()
{
    $this->template->build("ucp/ucp_view", $data);
}
如您所见,
$data
数组应该传递给视图。如果我在方法本身而不是构造函数中定义
$data['login\u prompt']
,那么:

function index()
{
    $data['login_prompt'] = $this->data['login_prompt'];
    $this->template->build("ucp/ucp_view", $data);
}
登录提示将更改为正确的已登录消息。但是,我不希望将这一行添加到应用程序中每个控制器的每个方法中

我发现了一个类似的问题,只需将传递给视图的
$data
数组更改为
$this->data
,如图所示。这可以工作,但会破坏我的应用程序的其他几个部分

我觉得这个错误很明显。我做错了什么?


向下滚动大约一半至“向视图添加动态数据”

尝试将视图中的
$Data['login\u prompt']
切换到
$login\u prompt


错误消息“未定义索引”指的是
“登录提示”
不是关联数组
$data
中的索引。代码点火器将分解传递给$this->load->view()的数组。类似于
extract()
的工作方式(php、ci)。

您有两个选项

您可以在
MY_Controller
上使用
$this->data
属性,然后确保将
$this->data
传递给所有视图

// MY_Controller
public $data;

public function __construct()
{
    if($this->is_logged_in()) 
    {
        $this->username = $this->session->userdata('username');
        $this->data['login_prompt'] = "Hi, " . $this->username . " " . anchor("account/logout", "Logout");
    }
}
// MY_Controller
public $global_data;

public function __construct()
{
    if($this->is_logged_in()) 
    {
        $this->username = $this->session->userdata('username');
        $this->global_data['login_prompt'] = "Hi, " . $this->username . " " . anchor("account/logout", "Logout");

        $this->load->vars($this->global_data);
    }
}
然后在我们的控制器中

// An example controller. By extending MY_Controller 
// We have the data property available
UcpController extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->data['Some extra variable'] = 'Foo';
        // Notice we are giving $this->data rather than $data...
        // this means we will have our login prompt included
        // as it is set by the parent class
        $this->template->build("ucp/ucp_view", $this->data);
    }
}
或者,您可以在
MY_Controller
中设置一个全局数组,然后使用
load->vars
方法使该数组可供所有视图使用

// MY_Controller
public $data;

public function __construct()
{
    if($this->is_logged_in()) 
    {
        $this->username = $this->session->userdata('username');
        $this->data['login_prompt'] = "Hi, " . $this->username . " " . anchor("account/logout", "Logout");
    }
}
// MY_Controller
public $global_data;

public function __construct()
{
    if($this->is_logged_in()) 
    {
        $this->username = $this->session->userdata('username');
        $this->global_data['login_prompt'] = "Hi, " . $this->username . " " . anchor("account/logout", "Logout");

        $this->load->vars($this->global_data);
    }
}

这不是我的问题所在。问题是构造函数中声明的$data['login\u prompt']不能被控制器中的任何方法访问,因此不能传递给视图。