CodeIgniter – 在视图中访问$ configvariables
我经常需要在视图中访问$configvariables。 我知道我可以将它们从控制器传递到load->view() 。 但是明确地做到这一点似乎过分了。 
 是否有某种方法或窍门从CI视图访问$configvariables,而不会打扰带有备用代码的控制器? 
  $this->config->item()工作正常。 
 例如,如果configuration文件包含$config['foo'] = 'bar'; 那么$this->config->item('foo') == 'bar' 
 另外,在整个CodeIgniter实例中,Common函数config_item()几乎在任何地方工作。 控制器,模型,视图,库,帮手,钩子,不pipe。 
你可以这样做:
 $ci = get_instance(); // CI_Loader instance $ci->load->config('email'); echo $ci->config->item('name'); 
  $this->config->item('config_var')不适用于我的情况。 
 我只能使用config_item('config_var'); 在视图中回显variables 
你的控制器应该从数据库,configuration等收集所有的信息。有很多好的理由坚持这一点。 一个很好的理由是,这将允许您很容易地更改信息的来源,而不必对您的观点进行任何更改。
 echo $this->config->config['ur config file'] 
如果你的configuration文件也来照片你必须像这样访问例如我包含一个app.php在configuration文件夹我有一个variables
 $config['50001'] = "your message" 
现在我想在我的控制器或模型中访问。
尝试以下两种情况下应该工作
情况1:
 $msg = $this->config->item('ur config file'); echo $msg['50001']; //out put: "your message"; 
案例2:
  $msg = $this->config->item('50001'); echo $msg; //out put: "your message" 
获取configuration项目
要从configuration文件中检索项目,请使用以下function:
$这 – > config->项( 'ITEM_NAME');
其中item_name是要检索的$ config数组索引。 例如,要获取您的语言select,您将执行此操作:
$ lang = $ this-> config-> item('language');
如果您试图获取的项目不存在,该函数将返回NULL。
如果使用$ this-> config-> load函数的第二个参数将configuration项分配给特定索引,则可以通过在$ this-> config- > item()函数。 例:
 // Loads a config file named blog_settings.php and assigns it to an index named "blog_settings" $this->config->load('blog_settings', TRUE); // Retrieve a config item named site_name contained within the blog_settings array $site_name = $this->config->item('site_name', 'blog_settings'); // An alternate way to specify the same item: $blog_config = $this->config->item('blog_settings'); $site_name = $blog_config['site_name']; 
设置configuration项
如果您想要dynamic设置configuration项目或更改现有项目,可以使用以下方法:
$ this-> config-> set_item('item_name','item_value');
其中item_name是要更改的$ config数组索引,item_value是其值。
每当我需要访问configurationvariables,我倾向于使用:$ this-> config-> config ['variable_name'];
我就是这么做的 在config.php中
 $config['HTML_TITLE'] = "SO TITLE test"; 
在应用程序/视图/ header.php(假设HTML代码)
 <title><?=$this->config->item("HTML_TITLE");?> </title> 
  
 
 $config['cricket'] = 'bat'; 在config.php文件中 
  $this->config->item('cricket')在视图中使用它