繁体   English   中英

codeigniter 的自定义配置文件

[英]Custom config file for codeigniter

CodeIgniter 非常新,尝试创建自定义配置文件以将特殊变量加载到我的应用程序中。

application/config/我创建了custom.php并在该文件中放置了以下代码:

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

$gender = array ('male','female'); 

?>

然后我打开application/config/autoload并更改以下代码:

$autoload['config'] = array();

/* TO: */ 

$autoload['config'] = array('custom');

我刷新我的应用程序并看到此错误:

Your application/config/custom.php file does not appear to contain a valid configuration array.

我打开了一些默认配置文件,但没有看到配置数组? 我做错了什么?

使用

$config['gender']= array ('male','female');

代替

$gender = array ('male','female');

用于获取配置项

$this->config->item('item_name');

其中item_name是您要检索的$config数组索引。

文档: CodeIgniter 用户指南 2.x CodeIgniter 用户指南 3.x

创建自定义配置文件:在“application/config/”下添加一个名为“custom_config.php”的新文件(或提供任何名称)并添加以下代码

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
//adding config items.
$config['gender'] = array('female', 'male');

加载自定义配置文件:-创建自定义配置文件后,我们需要加载它以获取它的项目。 对于加载自定义配置,我们有两种方式

*** 手动加载:- 我们可以在控制器/模型中手动加载配置文件,例如

$this->config->load('custom_config'); //or instead your file name.

*** 自动加载 :- 对于自动加载配置文件,请转到“application/config/autoload.php”并在 $autoload['config'] 中添加代码

$autoload['config'] = array('custom_config'); //or instead your file name.

代码点火器 3

第 1 步:创建自定义配置文件

/path/to/codeigniter/application/config/custom.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

$config['server'] = array (
    'host'      => 'example.com',
    'public_ip' => '93.184.216.34'
    );
?>

第二步:加载自定义配置文件

这可以在模型或控制器中完成。

// load the configuration file
$this->config->load('custom');

// retrieve the content of a specific configuration
$server = $this->config->item('server');

// $server is now set
echo $server['host']; // example.com

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM