Passing variables to all views. - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11) +--- Thread: Passing variables to all views. (/showthread.php?tid=61351) |
Passing variables to all views. - iM7md - 04-10-2015 Hello, I'm new to CodeIgniter and PHP in general, but everyone starts somewhere. I am coding a login/register billing software and wanted to know how to pass variables to all views in the application. I have "configuration" variables where it gets the values from the database like company name, logo location, etc. Thanks. RE: Passing variables to all views. - gadelat - 04-10-2015 you should put those in config file, then you can access these using config_item() in your views RE: Passing variables to all views. - CroNiX - 04-10-2015 He said they come from the database. Not all settings belong in a config file. Most clients don't have the ability to go editing php files. I do something like that with application specific config settings. I just create a library that triggers a method in __construct() that gets the data from the db and creates variables from them. That library gets autoloaded so the variables are available globally. It's fairly basic...something like: PHP Code: class Site_settings { Now if you autoload the site_settings library, $this->site_config is the array that's available to all other controllers/models/views/etc. PHP Code: echo $this->site_settings['company_slogan']; RE: Passing variables to all views. - iM7md - 04-10-2015 Thanks for the solution. I researched about it, and I saw that something like this is possible: Code: class MY_Controller extends CI_Controller { And then extending MY_Controller with all my controllers. Will this work? RE: Passing variables to all views. - casa - 04-11-2015 if you want to pass all the time the same variables in all the views and this variables not change, you can do like you say : extend CI_Controller with your MY_Controller and after all your controllers extend MY_Controller. Note that : My_Controler.php will be in directory named "core" of the directorys "application". No need to put it in the directory "core" of your directory "system". application/core/MY_Controller.php PHP Code: defined('BASEPATH') OR exit('No direct script access allowed'); PHP Code: defined('BASEPATH') OR exit('No direct script access allowed'); PHP Code: $v = $this->load->get_var('some_var') ; // or only call $some_var RE: Passing variables to all views. - InsiteFX - 04-11-2015 For variables that will not fit in a config file you can pass them global using: PHP Code: $data = array(); |