Auto-Loading Splints

Just as you specify resources such as libraries, helpers, models etc. to load automatically from the application/config/autoload.php file, you can as well load splint resources from this same file, provided that your loader is patched.

The following Splint resources can be auto-loaded:

  • Libraries
  • Models
  • Helpers
  • Configs
  • Views

To auto load splints, open the application/config/autoload.php file and look for or create the associative key splint in the $autoload array.

If you are using the Splint SDK as your Code Igniter distribution, you'll already have the associative key (splint) declared on the $autoload array at the bottom of the file.
Remember, $autoload[“splint”] is intended to be an associative array of splints to auto-load. the key of each element will be the vendor and package name combination of the splint while it's corresponding value is an array of arguments. The arguments you would pass to the $this→load→splint() function when loading the splint from a controller.

Auto-Loading Libraries

$autoload["splint"] = array("vendor/package_name" => array("+Library", $params, $alias));

The above will allow you use the library from anywhere with

$this->alias->method();
Note the prefix character(+) for specifying the auto-loaded library class. See the notes on Load Prefixes to know more about it.

Auto-Loading Models

$autoload["splint"] = array("vendor/package_name" => array("*Model", $alias));

The above will allow you use the model from anywhere with

$this->alias->method();

Auto-Loading Helpers

$autoload["splint"] = array("vendor/package_name" => array("%helper", $alias));

This will enable you call the functions declared in the helper from anywhere.

Auto-Loading Configs

$autoload["splint"] = array("vendor/package_name" => array("@config"));

Auto-Loading Views

$autoload["splint"] = array("vendor/package_name" => array("-view", $data));

This will result in all pages your Controllers send back to browsers having the specified view.

Conclusion

Assuming we have 3 libraries to auto-load which are:

  • zoey/bootstrap
  • francis94c/ci-preference
  • jonas/git

We could auto-load them as follows.

$params = array();
$autoload["splint"] = array (
  "zoey/bootstrap"           => array("+BootStrap", $params, "bootstrap"),
  "francis94c/ci-preference" => array("+CIPereferences", $params, "prefs"),
  "jonas/git"                => array("+Git", null, "git") 
)

This will allow us to use these libraries from anywhere like below:

// zoey/bootstrap
$bootstrap->loadHeaders();
// francis94c/ci-preference
$prefs->set("open", true);
$prefs->commit();
// jonas/got
$git->clone($url, $dir);

NEXT

PREVIOUS

Other Topics