Home >php教程 >php手册 >使用配置类定义 Codeigniter 全局变量

使用配置类定义 Codeigniter 全局变量

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-13 11:40:56829browse

之前提到的 CodeIgniter 引入自定义公共函数 这篇文章提到了公共函数实现,全局的变量也可以借助 helper 函数来实现。

不过,更为合适的方式可能要属用配置类定义了。

相关下载:CodeIgniter v1.7.3 开源PHP框架

CodeIgniter 默认有一个主配置文件,位于application/config/config.php 路径,其中定义了一堆框架级别的全局配置,一个名称为$config 的数组。

如果需要添加全局配置项,可以在这个文件中实现,考虑到自定义配置和框架配置的分离,建议新建一个文件 vars.php,然后做如下定义:

/**
 * 工作目录配置
 */
$config['src']['cache'] = FCPATH . '../src/cache';
$config['src']['modules'] = FCPATH . '../src/modules';
$config['src']['www'] = FCPATH . '../src/www';

使用时,通过以下代码在控制器中读取:

$src = $this->config->item('src');
$cache = $src['cache']

或者:

$src = $this->config->item('cache', 'src');

当然,你需要在 application/config/autoload.php 中自动加载这个配置文件。

参考资料:http://codeigniter.com/user_guide/libraries/config.html

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn