Table of Contents
Load Splint Libraries
Splint libraries can be loaded from anywhere you can access the load
object in Code Igniter.
The following shows how this can be done.
$this->load->splint("vendor_name/library_name", "+LibraryClassName" , $params, "alias"); // Library loaded and initialized with $alias. $this->alias->someMethod();
the $this→load→splint();
method call becomes available when you patch your Loader
class. Fortunately, the Splint command line tool does that automatically for you the moment you install a library.
Notice the +
character before LibraryClassName
in the above code. This tells splint to load a library from the specified package vendor_name/library_name
or rather search for a php
file in with the name LibraryClassName
from the libraries folder in the specified package and load it.
You can also load views
, models
, helpers
and configs
. All you need to do is use the required character as a prefix to the file name of the asset or php file you want to load from the package.
Load Prefixes
The table below shows the characters and what they instruct the splint loader to load/search for.
Character | To Load |
---|---|
+ | Library |
* | Model |
- | View |
@ | Config |
% | Helpers |
Examples
For a library called ci-preference created by a vendor named francis94c, we can load a Library
, Model
, View
, Config
, or Helper
in the manner shown below.
// Library. $this->load->splint("francis94c/ci-preference", "+CIPreferences", null, "prefo"); $this->prefo->get("a_key", "defaultVal"); // Model. $this->load->splint("francis94c/ci-preference", "*ModelClass", "alias"); $this->alias->someMethod(); // View. $this->load->splint("francis94c/ci-preference", "-view_header", array("text" => "Hello")); // Config. $this->load->splint("francis94c/ci-preference", "@config_file"); // Helper. $this->load->splint("francis94c/ci-preference", "%helper");
Load Multiple Libraries Or Resources
You can load libraries/resources at the same time using an array of associative arrays of arrays as shown below.
$autoload = array(); $autoload[] = array("library" => array("CIPreferences", null, "alias")); $autoload[] = array("model" => array("CIPrefModel", "alias")); // Model with alias. $autoload[] = array("model" => "CIPrefModel"); // Model without alias. $autoload[] = array("config" => "pref_config"); $autoload[] = array("helper" => "pref_helper"); $autoload[] = array("view" => "view_name"); $this->load->splint("francis94c/ci-preference", $autoload); // For loaded library. $this->alias->someMethod();
bind() function
Another way to load packages is by getting its Splint Object.
This is done using the bind()
function.
Ideally, a Splint object is meant to be used in a situation where you need to load multiple resources from one package (where supplying the full range of arguments to the splint()
function could be strenuous). The Splint object is a representation the package, that lets you load resources as you wish by specifying the package once.
$this->load->bind($splint, $bind);
Parameters
Parameter | Description | Required | Examples |
---|---|---|---|
$splint(string) | The splint package to load. | Yes | zoey/bootstrap |
$bind (null object) | The object that the specified $splint should be bound to. you do not need to declare this argument prior to calling the function. If omitted, the function will return the Splint object instead. | No | $this→load→bind( zoey/bootstrap, $bootstrap) |
For example, the zoey/bootstrap
package could be loaded with then bind
function and used as follows.
$this->load->bind("zoey/bootstrap", $bootstrap); $data = array("title" => "My Website"); $bootstrap->load->view("responsive_header", $data); $bootstrap->load->view("footer"); // OR $bootstrap = $this->load->bind("zoey/bootstrap"); $data = array("title" => "My Website"); $bootstrap->load->view("responsive_header", $data); $bootstrap->load->view("footer");
package() function
This is another method of loading called Hot Loading
. The package()
function allows you to specify a package name only that allows Splint to load resources specified automatically by the vendor of the Splint package.
$this->load->package($splint);
For instance, if a package's splint.json
descriptor file contains the below autoload
key defined along side other keys as follows.
This method of loading can be used if your package vendor has support for it. This you will usually find out from the package README.
If you would like to find out how vendors can specify what gets loaded when this function is called, See Descriptor Autoloading.