php codeigniter 的自定义配置文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20810891/
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
Custom config file for codeigniter
提问by Mitch Evans
Very new to CodeIgniter, trying to create a custom config file to load special variables into my application.
CodeIgniter 非常新,尝试创建自定义配置文件以将特殊变量加载到我的应用程序中。
in application/config/I created custom.phpand placed the following code in that file:
在application/config/我创建custom.php并将以下代码放入该文件中:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
$gender = array ('male','female');
?>
I then opened up application/config/autoloadand altered the following code:
然后我打开application/config/autoload并更改了以下代码:
$autoload['config'] = array();
/* TO: */
$autoload['config'] = array('custom');
I refresh my application and see this error:
我刷新我的应用程序并看到此错误:
Your application/config/custom.php file does not appear to contain a valid configuration array.
I opened up some of the default config files and don't see a configuration array? What am I doing wrong?
我打开了一些默认配置文件,但没有看到配置数组?我究竟做错了什么?
回答by Pranav C Balan
Use
用
$config['gender']= array ('male','female');
instead of
代替
$gender = array ('male','female');
For fetching config item
用于获取配置项
$this->config->item('item_name');
Where item_nameis the $configarray index you want to retrieve.
您要检索item_name的$config数组索引在哪里。
回答by ANHNNP
Creating custom config file: Add a new file under “application/config/” with named “custom_config.php” (or give any name) and add below code
创建自定义配置文件:在“application/config/”下添加一个名为“custom_config.php”的新文件(或提供任何名称)并添加以下代码
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
//adding config items.
$config['gender'] = array('female', 'male');
Loading custom config file:- After create custom config file we need to load it get it's items. for load custom config we have two ways
加载自定义配置文件:-创建自定义配置文件后,我们需要加载它以获取它的项目。对于加载自定义配置,我们有两种方式
*** Manual loading:- we can manual load config file in controller/model like
*** 手动加载:- 我们可以在控制器/模型中手动加载配置文件,例如
$this->config->load('custom_config'); //or instead your file name.
*** Autoloading :- for auto load config file go to “application/config/autoload.php” and add code in $autoload[‘config']
*** 自动加载 :- 对于自动加载配置文件,请转到“application/config/autoload.php”并在 $autoload['config'] 中添加代码
$autoload['config'] = array('custom_config'); //or instead your file name.

