-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Modularity in CodeIgniter PHP5
[h2]Plugin Introduction[/h2]
I've become sick and tired of CodeIgniter's [b]$this[/b]-centeredness and I want to use static PHP5 classes to access core CodeIgniter objects such as libraries, models, view, and db. So, I have written a very simple set of wrapper classes.
[b]Advantages:[/b]
- No more calling get_instance() when you need to access the framework inside a model, library, helper, or view
- More concise syntax
- Support for multiple database connections
[b]Requirements:[/b]
- CodeIgniter 1.5+
- PHP5+
[b]Installation:[/b] This is a plugin so no core hacking is required. Simply download [b][url=http://codeigniter.com/wiki/2928466684f73b3cba7a3510995bc94e/]modular_pi.zip[/url][/b] and extract it into your [b]application/plugins[/b] folder. Load it just like any other plugin.
[h2]How to use the Modularity Plugin[/h2]
[h3]Views[/h3]
[code]
$this->load->view('myview', $data);
View::show('myview', $data);
$pagetext = $this->load->view('myview', $data, TRUE);
$pagetext = View::parse('myview', $data); [/code]
[h3]Libraries[/h3]
[code]
$this->load->library('mylibrary'); $this->library->do_something();
Libs::e('mylibrary')->do_something(); // the library automatically loads if needed [/code]
[h3]Models[/h3]
[code]
$this->load->model('mydatamodel'); $this->mydatamodel->do_something();
Models::e('mydatamodel')->do_something(); // the model automatically loads if needed [/code]
[h3]Databases[/h3]
[code]
$this->load->database(); $query = $this->db->query($sql);
$query = DB::h()->query($sql); // the database automatically loads if needed
$dbh1 = $this->load->database('db1', TRUE); $dbh2 = $this->load->database('db2', TRUE); $query1 = $dbh1->query($sql); $query2 = $dbh2->query($sql);
$dbh1 = DB::h('db1')->query($sql); $dbh2 = DB::h('db2')->query($sql); [/code]
[h3]Inside libraries, helpers, or views[/h3]
[code]
$CI =& get_instance(); $CI->load->model('mymodel'); $CI->mymodel->do_something();
Models::e('mymodel')->do_something(); [/code]