php CodeIgniter - 在视图中访问 $config 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2631439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CodeIgniter - accessing $config variable in view
提问by AlexA
Pretty often I need to access $configvariables in views.
I know I can pass them from controller to load->view().
But it seems excessive to do it explicitly.
我经常需要访问$config视图中的变量。我知道我可以将它们从控制器传递到load->view(). 但明确地这样做似乎有些过分。
Is there some way or trick to access $configvariable from CI views without
disturbing controllers with spare code?
有什么方法或技巧可以$config从 CI 视图访问变量而不用备用代码干扰控制器吗?
回答by Phil Sturgeon
$this->config->item()works fine.
$this->config->item()工作正常。
For example, if the config file contains $config['foo'] = 'bar';then $this->config->item('foo') == 'bar'
例如,如果配置文件包含$config['foo'] = 'bar';那么$this->config->item('foo') == 'bar'
回答by Phil Sturgeon
Also, the Common function config_item()works pretty much everywhere throughout the CodeIgniter instance. Controllers, models, views, libraries, helpers, hooks, whatever.
此外,Common 函数config_item()在整个 CodeIgniter 实例中几乎无处不在。控制器、模型、视图、库、助手、钩子等等。
回答by Alexey Shein
You can do something like that:
你可以这样做:
$ci = get_instance(); // CI_Loader instance
$ci->load->config('email');
echo $ci->config->item('name');
回答by user1002232
$this->config->item('config_var')did not work for my case.
$this->config->item('config_var')不适用于我的情况。
I could only use the config_item('config_var');to echo variables in the view
我只能使用config_item('config_var');来回显视图中的变量
回答by Utah_Dave
Your controller should collect all the information from databases, configs, etc. There are many good reasons to stick to this. One good reason is that this will allow you to change the source of that information quite easily and not have to make any changes to your views.
你的控制器应该从数据库、配置等收集所有信息。有很多很好的理由坚持这一点。一个很好的理由是,这将允许您非常轻松地更改该信息的来源,而不必对您的视图进行任何更改。
回答by fangstar
回答by krishna_coolsofty
echo $this->config->config['ur config file']
If your config file also come to picture you have to access like this for example I include an app.php in config folder I have a variable
如果您的配置文件也出现在图片中,您必须像这样访问例如我在配置文件夹中包含一个 app.php 我有一个变量
$config['50001'] = "your message"
Now I want access in my controller or model .
现在我想访问我的控制器或模型。
Try following two cases one should work
尝试以下两种情况,一种应该有效
case1:
情况1:
$msg = $this->config->item('ur config file');
echo $msg['50001']; //out put: "your message";
case2:
案例2:
$msg = $this->config->item('50001');
echo $msg; //out put: "your message"
回答by The website-lab
Whenever I need to access config variables I tend to use: $this->config->config['variable_name'];
每当我需要访问配置变量时,我倾向于使用: $this->config->config['variable_name'];
回答by Rick
$config['cricket'] = 'bat';in config.php file
$config['cricket'] = 'bat';在 config.php 文件中
$this->config->item('cricket')use this in view
$this->config->item('cricket')使用这个视图
回答by Mannu saraswat
If you are trying to accessing config variable into controller than use
如果您尝试将配置变量访问到控制器而不是使用
$this->config->item('{variable name which you define into config}');
If you are trying to accessing the config variable into outside the controller(helper/hooks) then use
如果您尝试将 config 变量访问到控制器外部(helper/hooks),则使用
$mms = get_instance();
$mms->config->item('{variable which you define into config}');


