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

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

Jun 13, 2016 am 11:40 AM
codeigniteruseoverall situationvariabledefinitionintroducementionedofkindcustomizeConfiguration

之前提到的 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use