![]() |
Global variable in CI - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Global variable in CI (/showthread.php?tid=60179) |
Global variable in CI - El Forum - 02-01-2014 [eluser]anishpsla[/eluser] Is it possible to add global variable to CI ? For example, I want to use some parameter like site name to all page titles. Making it available all over the application will help. Global variable in CI - El Forum - 02-01-2014 [eluser]Tpojka[/eluser] There is more then one way you can do that. On my mind are two immediatelly: 1. save it in - application/constants.php 2. Declare variable in core/MY_Controller.php Example Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Global variable in CI - El Forum - 02-01-2014 [eluser]CroNiX[/eluser] Another way is with a config file, and then autoload it. http://ellislab.com/codeigniter/user-guide/libraries/config.html Global variable in CI - El Forum - 02-01-2014 [eluser]ivantcholakov[/eluser] Yet another way you may find within this discussion: http://ellislab.com/forums/viewthread/241311/ Global variable in CI - El Forum - 02-02-2014 [eluser]jonez[/eluser] If you are setting application constants I'd recommend using a config file. In my app I auto-load /config/app.php that contains constants like the application name, support email, etc. Then you can access those variables using $this->config->item( 'name' ). If they are true constants I'd add them to /config/constants.php and name them in all uppercase like the others. Global variable in CI - El Forum - 02-03-2014 [eluser]anishpsla[/eluser] Thanks friends, thanks for your valuable info. Now I know how to set Global variables in CI. |