CodeIgniter Global Variable

In this tutorial, we will learn to create Global Variables in CodeIgniter. Now Why Exactly do we use it..?? Its all because Global Variables are available in every scope and holds static information. It makes the information very convenient and simple to update and can probably be modified from anywhere.

Now when it comes to define them in CodeIgniter, there are several ways do that. I’ve listed some of them below:

1.  Create your own file in application/libraries in which class constructor contains an array as an argument. Now create a new file in /application/config with same name as given in application/libraries and declare your global variables in it. Now to use these variables, autoload the newly created library.

2.  Create your own file in application/core and declare the global variables in it. Than in controller you need to extend your file name instead of CI_Controller.

3.  If the Global Variables are true constants, just add them in application/config/constants.php file and name them in all uppercase like the others are defined.

4.  If you want to set application constants create new config file and add the variables. Now load it as $this->config->load(‘filename’); And access those variables as

 $this->config->item(‘variable_name’); .


Steps To Create Global Variables Using First Method:

Step 1:  First of all, open application/libraries and create a custom class name globals.php.  It contains a constructor function which contains an array as an argument.

 $value) {
$data[$key] = $value;
}

// Make instance of CodeIgniter to use its resources
$CI = & get_instance();

// Load data into CodeIgniter
$CI->load->vars($data);
}

}

?>

Copy


Download The Script File

Save the project in your server directory and use the path given below to run the script.

 

DOWNLOAD SCRIPT   

 

http://localhost/ci_global_variable/

Copy


Step 2: Now to make config file, open application/config and create file as globals.php and write the code given below. This file contains the config variable which will be passed as an array to constructor of Globals class where its keys and values are stored in $data.

Copy

When constructor function loads, it will take the config variables from the config file in order to use these variables anywhere.

Note:  Name of the above file must be same as the class created in libraries folder otherwise the code will not work.

Step 3: But before using these variables we have to autoload the newly created library globals as given below.

$autoload['libraries'] = array('globals');

Copy


Now, we are ready to use these global variables for our code.

Controllers : ci_global_variable_tutorial.php

Create controller which loads the view page.

load->view('show_global_variables');
}
}
?>

Copy

Views : show_global_variables.php

In view page, we can use global variables according to our need.

"."Click here to go to blog page"."";
?>

Copy


Conclusion:

So now you know different ways to use Global variable in CodeIgniter. Hope you like the tutorial, keep reading our other blogs posts for more coding tricks. 🙂

 

https://www.formget.com/codeigniter-global-variable/

Codeigniter: fatal error call to undefined function mysqli_init()

sudo apt-get install php7.2-mysql

sudo service apache2 restart

CodeIgniter 500 Internal Server Error

https://expressionengine.com/forums/archive/topic/167202/requested-url-was-not-found-on-this-server/P15

apache2.conf

 

    Require all granted

    AllowOverride All

https://stackoverflow.com/a/49408029/11971064

Try this to your .htaccess file:

  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?/$1 [L]

https://stackoverflow.com/a/39261712/11971064

I know I am late, but this will help someone.

Check if rewrite engine is enabled.

If not, enable rewrite engine and restart server.

sudo a2enmod rewrite
sudo service apache2 restart