Welcome Guest, Not a member yet? Register   Sign In
Global variable in CI
#1

[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.
#2

[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');

class MY_Controller extends CI_Controller {

public $siteName = 'Name of Site';

        public function __construct()
{
  parent::__construct();
}
}

/* End of file MY_Controller.php */
/* Location: ./application/core/MY_Controller.php */
Than in controller you need to extend MY_Controller, NOT CI_Controller:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Some_controller extends MY_Controller {

public function __construct()
{
  parent::__construct();
}

        public function index()
        {
                echo $this->siteName;
        }

}

/* End of file some_controller.php */
/* Location: ./application/controllers/Some_controller.php */
#3

[eluser]CroNiX[/eluser]
Another way is with a config file, and then autoload it.
http://ellislab.com/codeigniter/user-gui...onfig.html
#4

[eluser]ivantcholakov[/eluser]
Yet another way you may find within this discussion: http://ellislab.com/forums/viewthread/241311/
#5

[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.
#6

[eluser]anishpsla[/eluser]
Thanks friends, thanks for your valuable info. Now I know how to set Global variables in CI.




Theme © iAndrew 2016 - Forum software by © MyBB