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

WBOY
Release: 2016-07-13 10:28:30
Original
951 people have browsed it

CodeIgniter中公共函数不能追加,可以通过 helper 辅助函数实现。
创建 common_helper.php 文件,定义所需公共函数,存放至 application/helpers 目录中。
在 application/config/autoload.php 中配置 $autoload['helper'] = array('common'); 即可。

全局的变量也可以借助 helper 函数来实现。不过,更为合适的方式可能要属用配置类定义了

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 中自动加载这个配置文件。

www.bkjia.com true http://www.bkjia.com/PHPjc/788622.html TechArticle CodeIgniter中公共函数不能追加,可以通过 helper 辅助函数实现。 创建 common_helper.php 文件,定义所需公共函数,存放至 application/helpers 目录中...
Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!