How to define global variable in codeigniter?

by herminia_bruen , in category: PHP Frameworks , 2 months ago

How to define global variable in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by muriel.schmidt , 2 months ago

@herminia_bruen 

In CodeIgniter, you can define a global variable by creating a custom configuration file in the application/config directory.

  1. Create a new configuration file: You can create a new configuration file by creating a new PHP file in the application/config directory. For example, you can create a file named custom_config.php.
  2. Define the global variable: In the configuration file you created, define the global variable using the $config array. For example, you can define a global variable named base_url like this:
1
2
3
4
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

$config['base_url'] = 'http://localhost/project/';


  1. Load the custom configuration file: To use the global variable in your application, you need to load the custom configuration file. You can do this by using the config class in your controller or model. For example:
1
2
$this->config->load('custom_config');
$base_url = $this->config->item('base_url');


By following these steps, you can define and use global variables in CodeIgniter.