Welcome Guest, Not a member yet? Register   Sign In
Global variables in Codeigniter
#1

[eluser]bleu[/eluser]
How exactly does one create Global variables in Codeigniter if I create it in one controller file for that files functions how can I use it in another controller and its functions
#2

[eluser]animatora[/eluser]
If you create it in a one Controller it will be available for that controller only. What I do is create a My_Controller class in core/ folder that extends CI_Controller. If you define the variable there and extends My_Controller by all your controller instead of CI_Controller you will have it available to all.

PS. If you use custom controller that is extending CI_Controller, don't forget to call the parent::__construct() in your constructor, so you get all the functionality from CI_Controller.
#3

[eluser]LuckyFella73[/eluser]
To put the variables into your MY_controller is a way to do that - if
you may need the values in your models too it's better to place that stuff
in a config file which is accessable in controllers and models.
#4

[eluser]bleu[/eluser]
If I place my variables in MY_Controller will I be able to access them in my views without passing them through the controller
#5

[eluser]LuckyFella73[/eluser]
Yes they are.
#6

[eluser]bleu[/eluser]
[quote author="LuckyFella73" date="1331294324"]Yes they are.[/quote]
Thanks ,

I am using them like below

in my MY_Controller
Code:
<?php

$Is ="";
$s23 = "";
$s1 ="";
$s2 = "";
$st2 ="";

      
    
class  MY_Controller  extends  CI_Controller  {
    
    function MY_Controller ()  {
        parent::__construct();


and calling them in my view or any other place as

Code:
echo $GLOBALS['Is'];
echo $GLOBALS['s23'];
echo $GLOBALS['s1'];
echo $GLOBALS['st2'];



Is this correct?
#7

[eluser]LuckyFella73[/eluser]
I now prefer to set my "global" values in a config file.
When I did it in MY_Controller it looked like that:

Code:
class MY_Controller extends CI_Controller {

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

  $this->my_global_var_1 = 'Value 1';
  $this->my_global_var_2 = 'Value 2';
}
}

Your Controller now has to extend MY_Controller. ( usually I have a MY_Controller
extending CI_Controller, then a Public_Controller extending MY_Controller
and all my frontend controllers are extending Frontend_Controller)

Now you can use your vars from MY_Controller in your views like:
Code:
echo $this->my_global_var_1;
#8

[eluser]bleu[/eluser]
Thanks for the above part I believe that I can now set and access my variables as such
Code:
$this->my_global_var_1 ="hi1";  
echo $this->my_global_var_1;



Also Is the below wrong from a security point of view




[quote author="bleu" date="1331306451"][quote author="LuckyFella73" date="1331294324"]Yes they are.[/quote]
Thanks ,

I am using them like below?

in my MY_Controller
Code:
<?php

$Is ="";
$s23 = "";
$s1 ="";
$s2 = "";
$st2 ="";

      
    
class  MY_Controller  extends  CI_Controller  {
    
    function MY_Controller ()  {
        parent::__construct();


and calling them in my view or any other place as

Code:
echo $GLOBALS['Is'];
echo $GLOBALS['s23'];
echo $GLOBALS['s1'];
echo $GLOBALS['st2'];



Is this correct?
[/quote]
#9

[eluser]Aken[/eluser]
1) You should consider learning more about basic PHP before CodeIgniter. It's clear you don't have a lot of understanding yet.

2) To answer your question, if the variables don't change, just create a config file and access them as CI config items. http://ellislab.com/codeigniter/user-gui...onfig.html




Theme © iAndrew 2016 - Forum software by © MyBB