Skip to content

Php5 magic Autoloader

World Wide Web Server edited this page Jul 4, 2012 · 21 revisions

[h2]Plugin Introduction[/h2]

A simple Php5 Front-End for CI Loader Class. Bye bye [b]$this[/b].

[b]Advantages:[/b]

  • No more calling get_instance() when you need to access the framework inside a model, library, helper, or view
  • Support for multiple database connections
  • More concise syntax
  • Lazy-load support

[b]Requirements:[/b]

  • CodeIgniter 1.5+
  • PHP5+

[b]Version 1.2 Changelog: (downloads: 23)[/b]

  • Added Support Codeigniter <= 1.7.3
  • Added Plugins()
  • Added Configs()
  • Added Vars()
  • Added Helpers()
  • Lightweight coding style.
  • All logic work delegated to CI Core for compatibility propose
  • Added support for ALL original functions params ($this->load->xxx('name', 'args1', 'args2'))
  • Renamed "Libs" in "Libraries" (a little longer, but reading style come first!).

[b]Version 1.3 Changelog: (downloads: 7)[/b]

  • Added more controls for downsize overhead
  • Added Php5 Helpers Mode :)
  • Added Plugin suffix

[b]Version 1.4 Changelog: (downloads 8)[/b]

  • Added Langs()
  • Added Modularity for Configs, Helpers, Langs (es. Configs('rss')->line('ip');)
  • Libraries re-renamed in "Libs" by popular acclaim (lol)
  • Added more and more and more controls for downsize overhead
  • Better Docs

[b]Version 1.5 Changelog:[/b]

  • Added Libs('uri')->something(); support for CI parent instance
  • Check Control for Core CI Libs Autoloaded (Uri, Input, Config etc...). Now more faster
  • Created CI Wiki page

[b]Credits[/b] Based on Modularity Plugin by Jonathon Hill (http://codeigniter.com/forums/viewthread/99960/P15/)

[b]Installation:[/b] This is a plugin so no core hacking is required. Simply download, extract it into your [b]application/plugins[/b] folder. (auto)Load it just like any other plugin.

[h2]How to use the Plugin[/h2]

[h3]Views[/h3]

[code]

old way

$this->load->view('news_view', $data);

new way

Views('news', $data); [/code]

[h3]Libraries[/h3]

[code]

old way

$this->load->library('mylibrary'); $this->mylibrary->do_something();

new way

Libs('mylibrary')->do_something(); // the library automatically loads if needed

core library

Libs('input')->post('name'); Libs('output')->set_header('HTTP/1.0 200 OK'); Libs('session')->userdata('id'); [/code]

[h3]Models[/h3]

[code]

old way

$this->load->model('news_model'); $this->news_model->do_something();

new way

Models('news')->do_something();// the model automatically loads if needed [/code]

[h3]Langs[/h3]

[code]

old way

$this->load->language('filename'); $this->lang->line('language_key');

new way

Langs('filename')->line('language_key');// the language automatically loads if needed

alternative new way

Langs('filename'); Langs()->line('language_key'); [/code]

[h3]Plugins[/h3]

[code]

old way

$this->load->plugin('captcha'); $this->captcha->do_something();

new way

Plugins('captcha')->do_something();// the plugin automatically loads if needed [/code]

[h3]Configs[/h3]

[code]

old way

$this->config->load('filename'); $this->config->item('itemname');

new way

Configs('filename')->item('itemname'); // the config automatically loads if needed [/code]

[h3]Helpers[/h3]

[code]

old way

$this->load->helper('date'); echo now();

new way

echo Helpers('date')->now(); // the helper automatically loads if needed

alternative new way

Helpers('date'); echo now(); [/code]

[h3]Vars[/h3]

[code]

old way

$this->load->vars(array('page_type' => 'section'));

new way

Vars(array('page_type' => 'section')); [/code]

[h3]Databases[/h3]

[code]

old way - default database

$this->load->database(); $query = $this->db->query($sql);

new way

$query = DBS()->query($sql);// the "default" database automatically loads if needed [/code]

[h3]Multiple database connections[/h3]

[code]

old way

$dbh1 = $this->load->database('db1', TRUE); $dbh2 = $this->load->database('db2', TRUE); $query1 = $dbh1->query($sql); $query2 = $dbh2->query($sql);

new way

$dbh1 = DBS('db1')->query($sql);// the "db1" database automatically loads if needed $dbh2 = DBS('db2')->query($sql);// the "db2" database automatically loads if needed [/code]

[h3]Inside libraries, helpers, or views[/h3]

[code]

old way

$CI =& get_instance(); $CI->load->model('news_model'); $CI->news_model->do_something();

new way

Models('news')->do_something(); [/code]

Clone this wiki locally