![]() |
Global variables in Codeigniter - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Global variables in Codeigniter (/showthread.php?tid=49944) |
Global variables in Codeigniter - El Forum - 03-08-2012 [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 Global variables in Codeigniter - El Forum - 03-08-2012 [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. Global variables in Codeigniter - El Forum - 03-08-2012 [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. Global variables in Codeigniter - El Forum - 03-09-2012 [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 Global variables in Codeigniter - El Forum - 03-09-2012 [eluser]LuckyFella73[/eluser] Yes they are. Global variables in Codeigniter - El Forum - 03-09-2012 [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 and calling them in my view or any other place as Code: echo $GLOBALS['Is']; Is this correct? Global variables in Codeigniter - El Forum - 03-09-2012 [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 { 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; Global variables in Codeigniter - El Forum - 03-11-2012 [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"; 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 and calling them in my view or any other place as Code: echo $GLOBALS['Is']; Is this correct? [/quote] Global variables in Codeigniter - El Forum - 03-11-2012 [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-guide/libraries/config.html |