A core object loader and accessor for CodeIgniter
PHP
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
helpers
.gitignore
README.md
spark.info

README.md

Overview

Loaders is a collection of loaders and accessors for CodeIgniter's core objects.

After you write enough CodeIgniter applications, writing $this and manually loading stuff gets old. Loaders is an easier, globally-accessible way to access core CodeIgniter objects such as libraries, models, view, and db.

Originally conceived as a plugin called "Modularity", this has been reborn as a CodeIgniter 2.0-compatible helper.

Features

  • Auto-load libraries, models, databases
  • Eliminates need to call get_instance() when you need to access framework objects inside a model, library, helper, or view
  • Concise syntax
  • Supports multiple database connections
  • Requires PHP5 & CodeIgniter 1.5+

Installation

Install and load Loaders as you would any other CodeIgniter helper.

Or, use the Sparks package installer:

php tools/spark install -v0.0.1 loaders

Usage

Views

The normal way to load and display a view is like so:

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

With the View loader, do it this way:

Views::show('myview', $data);

Or if you want to return a string instead of outputting the view, do this:

$pagetext = Views::parse('myview', $data);

Libraries

The old way to load libraries was like this:

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

With the library loader, you can just do this:

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

Models

To fetch data with a model, we used to do this:

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

Now, with the model loader, it works like this:

Model('mydatamodel')->do_something(); // the model automatically loads if needed

Databases

The old way to run a query on the default database connection:

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

Using the database loader, it is easier:

$query = Database()->query($sql); // the database automatically loads if needed

Maybe you have multiple connections?

$query1 = Database('db1')->query($sql);
$query2 = Database('db2')->query($sql);

Global access

These loaders work anywhere in exactly the same way, including libraries, helpers, and views.

Previously, you had to get an instance of the $CI object to work with, which can be a drag:

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

Now you don't have to call get_instance() anymore!

Model('mymodel')->do_something();