Codeigniter Driver

Next Page Previous Page

Drivers in CodeIgniter are some special libraries, these libraries contain parent classes and every parent class contains child classes, the parent class, and the child class can have access to each other but a child class cannot directly access the other child classes from the same and the different parent.

Initialization of a Driver:

Syntax:

$this->load->driver('class_name');

Parameters:

  • Class_name: any class name is added to the class_name parameter.

Example:

$this->load->driver(‘your_class_name’); 

Invoking a method from class:

$this->your_class_name>a_method(); 

Calling child classes through parent class:

$this->parent_class->first_child_class->a_method();  

$this->parent_class->second_child_class->a_method();

Creating our own custom Driver:

Let’s follow three simple and quick steps to get our own driver.

  1. Create a file structure
  2. Create a  driver list
  3. Create custom drivers

Step 1: Creating a file structure for our custom drivers.

//System

        /libraries  

                 /My_Custom_driver  

                           //My_custom_driver.php  

                                       /custom drivers  

                                                 //My_first_Custom_driver.php 
  • My_Custom_driver - it is our class.
  • My_custom_driver.php - this is a parent driver.
  • My_first_Custom_driver.php - this is our child driver.

 

Step 2: Creating a driver’s list for our custom drivers.

My_custom_driver.php

<?php  

defined('BASEPATH') OR exit(‘ Direct scripts are not allowed');  

class CI_My_custom_driver extends CI_Driver_Library  

{  

    function __construct()  

    {  

        $this->valid_drivers = array('my_first_custom_driver');  

    }  

    function index()  

    {  

        echo "<h1>This is our custom Parent Driver</h1>";  

    }  

     } 



Creating a child custom driver:

<?php  

defined('BASEPATH') OR exit(‘ Direct scripts are not allowed');  

class My_driver_first_custom_driver extends CI_Driver  

{  

    function custom_first()  

    {  

        echo "<h1>This is our first custom Child Driver</h1>";  

    }  

     }  

?> 

Calling our custom drivers in a controller:

<?php  

defined('BASEPATH') OR exit(‘ Direct scripts are not allowed');  




    class Mydrive extends CI_Controller  

 {  

    function __construct()  

    {  

             parent::__construct();  

         $this->load->driver('my_custom_driver');  

      }  

    public function invoke1()  

    {  

       $this->'my_custom_driver'>index();  

}  

    public function invoke2()  

    {       

       $this->my_driver->my_first_custom_driver->custom_first();  

    }  

}?> 

Summary:

  • Drivers are the special libraries in Codeigniter that contain parent and range of child categories in it.
  • Child categories can easily get access to their parent category but they cannot access their sibling’s categories.
  • CodeIgniter offers a chic syntax in the controller to access the driver libraries easily.
  • Drivers take place under the system/libraries/ directory, they contain their own sub-categories and classes.
  • A subdirectory can have its child classes inside a directory folder.
Next Page Previous Page

Treding Tutorial

Popular Tutorials

to Learn now