Home > Article > Backend Development > Defining Codeigniter global variables using configuration classes_PHP tutorial
The previously mentioned CodeIgniter introduces custom public functions. This article mentions public function implementation, and global variables can also be implemented with the help of helper functions.
However, a more appropriate way may be to use configuration class definition.
Related downloads: CodeIgniter v1.7.3 open source PHP framework
CodeIgniter has a main configuration file by default, located in the application/config/config.php path, which defines a bunch of framework-level global configurations, one named Array of $config.
If you need to add global configuration items, you can do it in this file. Considering the separation of custom configuration and framework configuration, it is recommended to create a new file vars.php, and then define it as follows:
/** * 工作目录配置 */ $config['src']['cache'] = FCPATH . '../src/cache'; $config['src']['modules'] = FCPATH . '../src/modules'; $config['src']['www'] = FCPATH . '../src/www';
When used, read in the controller through the following code:
$src = $this->config->item('src'); $cache = $src['cache']
or:
$src = $this->config->item('cache', 'src');
Of course, you need to automatically load this configuration file in application/config/autoload.php.
Reference material: http://codeigniter.com/user_guide/libraries/config.html