Codeigniter Loader Class Library


Codeigniter Loader Class Library – Loader class is initialized automatically by the system that’s why there is no need to do manually. This library provides various functions which are used to load elements. These elements can be library or class, Views file, Drivers, Helpers, Models or your own files. Here in this tutorial, we are going to explain how to use loader class library.


Codeigniter loader class library | Example

Let us understand how loader class library works in codeigniter with examples.

Functions:-

There are following functions available in loader class library. Now we will explain one by one.

  • 1. Application “Packages”.
  • 2. Package view files.

1. Application “Packages”.

Here is simple demo of application “Packages”.

Application package allows user to create own libraries, views, controller, models, helpers, config files in third party folder.

Codeigniter Loader Class Library

2. Package view files.

Here is simple demo of package view files.

How to create own view file see in example:-

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class loader_controller extends CI_Controller 
{
public function myPackage()
{
$this->load->add_package_path(APPPATH.'third_party/myApp', FALSE);
$this->load->view('welcome_message'); 
}
}
?>

output will be like this

Class reference:-

There are following references available in loaded class library. Now we will explain.

1. Library.

This reference is used to only load core classes.

library($library[$params = NULL[$object_name = NULL]])
  • Parameters.
  • $library (mixed) : Library name as a string or an array with multiple libraries
  • $params (array) : Optional array of parameters to pass to the loaded library’s constructor
  • $object_name (string) : Optional object name to assign the library to
  • Returns : CI_Loader instance (method chaining)
  • Return type : CI_Loader
Example.
$this->load->library('email');
$this->load->library(array('email', 'table'));//load multiple library using array.
$this->load->library('config', NULL, 'my_config');//we can set own name of library.
$this->my_config

2. Driver.

We used this reference to load only driver library.

driver($library[$params = NULL[$object_name]])
  • Parameters.
  • $library (mixed) : Library name as a string or an array with multiple libraries
  • $params (array) : Optional array of parameters to pass to the loaded library’s constructor
  • $object_name (string) : Optional object name to assign the library to
  • Returns : CI_Loader instance (method chaining)
  • Return type : CI_Loader
Example.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class loader_controller extends CI_Controller 
{
public function myclass()
{		
$result = $this->load->driver('session');
print_r($result);
}
}
?>
	
$result = $this->load->driver(array('session', 'cache'));
print_r($result);
	
$config = array('sess_driver' => 'cookie',
'sess_encrypt_cookie'  => true,
'encryption_key' => 'mysecretkey');
$result = $this->load->driver('session', $config);
print_r($result);

Output will be like this of above code.

3. Views.

This reference is used to load only views page.

view($view[$vars = array()[return = FALSE]])
  • Parameters.
  • $view (string) : View name
  • $vars (array) : An associative array of variables
  • $return (bool) : Whether to return the loaded view
  • Returns : View content string if $return is set to TRUE, otherwise CI_Loader instance.
  • Return type : Mixed
Example.
$this->load->view('View_file_name');
$this->load->view('View_file_name', '', TRUE);

4. Vars.

This reference is used to check associative array as input and generate variable using php function.

vars($vars[$val = ''])
  • Parameters.
  • $vars (mixed) : An array of variables or a single variable name
  • $val (mixed) : Optional variable value
  • Returns : CI_Loader instance (method chaining).
  • Return type : CI_Loader

4. Get vars.

This is use to check array of variables available in your views file.

get_var($key)
  • Parameters.
  • $key (string) : Variable name key
  • Returns : Value if key is found, NULL if not.
  • Return type : mixed

5. Clear vars.

This reference is used to clear cached views variables.

clear_vars()
  • Parameters.
  • Returns : CI_Loader instance (method chaining).
  • Return type : CI_Loader

6. Models.

This reference is used to only load core classes.

model($model[$name = ''[$db_conn = FALSE]])
  • Parameters.
  • $model (mixed) : Model name or an array containing multiple models
  • $name (string) : Optional object name to assign the model to
  • $db_conn (string) : Optional database configuration group to load
  • Returns : CI_Loader instance (method chaining)
  • Return type : CI_Loader
Example.
$this->load->model('model_name');

7. Database.

This method is used to load the database.

database([$params = ''[$return = FALSE[$query_builder = NULL]]])
  • Parameters.
  • $params (mixed) : Database group name or configuration options
  • $return (bool) : Whether to return the loaded database object
  • $query_builder (bool) : Whether to load the Query Builder
  • Returns : Loaded CI_DB instance or FALSE on failure if $return is set to TRUE, otherwise CI_Loader instance (method chaining)
  • Return type : Mixed

8. Helper.

This method is used to load the helper.

helper($helpers)
  • Parameters.
  • $helpers (mixed) : Helper name as a string or an array containing multiple helpers
  • Returns : CI_Loader instance (method chaining)
  • Return type : CI_Loader

9. File.

This is a file loading method. Put the file path and file name in first parameter. It will open and read the file.

file($path[$return = FALSE])
  • Parameters.
  • $path (string) : File path
  • $return (bool) : Whether to return the loaded file
  • Returns : File contents if $return is set to TRUE, otherwise CI_Loader instance.
  • Return type : Mixed

10. Language.

This class is an alias of the language loading method.

language($files[$lang = ''])
  • Parameters.
  • $files (mixed) : Language file name or an array of multiple language files
  • $lang (string) : Language name
  • Returns : CI_Loader instance.
  • Return type : CI_Loader

11. Config.

This class is an alias of the config file loading method and return always boolean result.

config($file[$use_sections = FALSE[$fail_gracefully = FALSE]])
  • Parameters.
  • $files (string) : Configuration file name
  • $use_sections (bool) : Whether configuration values should be loaded into their own section
  • $fail_gracefully (bool) : Whether to just return FALSE in case of failure
  • Returns : TRUE on success, FALSE on failure.
  • Return type : Bool

12. is_Loaded.

we use this method to allows user to check whether class has been already loaded or not.

is_loaded($class)
  • Parameters.
  • $class (string) : Class name
  • Returns : Single ton property name if found, FALSE if not.
  • Return type : Mixed
Example.
$this->load->library('form_validation');
$this->load->is_loaded('Form_validation'); // returns 'form_validation'
$this->load->library('form_validation', $config, 'Loaded_library');
$this->load->library('form_validation');
$this->load->is_loaded('Form_validation'); // returns 'Loaded_library'

13. Add package path.

we use this method to load application package or adding a package path.

add_package_path($path[$view_cascade = TRUE])
  • Parameters.
  • $path (string) : Path to add
  • $view_cascade (bool) : Whether to use cascading views
  • Returns : CI_Loader instance.
  • Return type : CI_Loader
Example.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class loader_controller extends CI_Controller 
{
public function myPackage()
{
$this->load->add_package_path(APPPATH.'third_party/myApp', FALSE);
$this->load->view('welcome_message'); 
}
}
?>

14. Remove package path.

we use this method to removing a package path.

remove_package_path([$path = ''])
  • Parameters.
  • $path (string) : Path to remove
  • Returns : CI_Loader instance.
  • Return type : CI_Loader
Example.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class loader_controller extends CI_Controller 
{
public function myPackage()
{
$this->load->remove_package_path(APPPATH.'third_party/myApp', FALSE);
}
}
?>

14. Get package paths.

Its return all availble package path.

get_package_paths([$include_base = TRUE])
  • Parameters.
  • $include_base (bool) : Whether to include BASEPATH
  • Returns : An array of package paths.
  • Return type : array
Example.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class loader_controller extends CI_Controller 
{
public function myPackage()
{
$this->load->get_package_paths(APPPATH.'third_party/myApp', FALSE);
}
}
?>

Advertisements

Add Comment

📖 Read More