Echo配置项目在视图中?

时间:2013-07-01 20:03:26

标签: php codeigniter

我试图将一个配置变量回显到一个视图中,我不相信我正确地做到了,但是在这里阅读答案似乎我正确地做到了。

在我的配置文件夹中,我有以下文件:

  

帐户/ googleplus.php

在该文件中我有我的变量:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');    
$config['googleplus_client_id']     = "123456789";

在我的控制器中,我然后加载配置文件,然后加载视图:

    function __construct()
    {
        parent::__construct();
    $this->load->config(array('account/account', 'account/googleplus'));
    }
function index()
{
    $this->load->view('sign_in');
}

然后在我看来,我像这样回应变量:

<?php echo $this->config->item('googleplus_client_id');

我遇到的问题是它不会回应内容。我是否必须以某种方式将数据传递给视图,还是应该将其自身解决?

抛出错误:

  

配置文件Array.php没有   存在。

2 个答案:

答案 0 :(得分:2)

我认为它是因为$this变量,它与调用它的位置相关(在控制器,模型或视图中),因此控制器中的$this与{{1}不同在一个视图中。

另外,视图通常不是类,因此$this不是类。

相反,您可以将配置传递为:

控制器:

$this

视图:

function __construct()
{
    parent::__construct();
    $this->load->config('account/googleplus'); 
}
function index()
{
    $data = array('googleplus_client_id' => $this->config->item('googleplus_client_id'));
    $this->load->view('sign_in', $data);
}

答案 1 :(得分:2)

这是因为$ this-&gt; load-&gt; config()不接受数组参数。你应该做

$this->load->config('account/account');
$this->load->config('account/googleplus');

在您的控制器中。然后你可以打电话

$this->config->item()
在您的视图文件中

。根据{{​​3}}中的文档,它应该是

$this->config->load()

而不是

$this->load->config()
相关问题