Tutorialsplane

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”.

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.

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]])
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]])
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]])
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 = ''])

4. Get vars.

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

get_var($key)

5. Clear vars.

This reference is used to clear cached views variables.

clear_vars()

6. Models.

This reference is used to only load core classes.

model($model[$name = ''[$db_conn = FALSE]])
Example.
$this->load->model('model_name');

7. Database.

This method is used to load the database.

database([$params = ''[$return = FALSE[$query_builder = NULL]]])

8. Helper.

This method is used to load the helper.

helper($helpers)

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])

10. Language.

This class is an alias of the language loading method.

language($files[$lang = ''])

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]])

12. is_Loaded.

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

is_loaded($class)
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])
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 = ''])
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])
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);
}
}
?>

Models

Connect Database

Helpers

Libraries

Helper Reference

Library Reference

Database Reference