CodeIgniter Driver

 

What are Drivers

These are special type of library that has a parent class and many child classes. These child classes have access to the parent class, but not to their siblings. It enables you to make more elegant classes and more elegant syntax inside your controller.

Drivers are found in system/libraries folder in CodeIgniter folder.


Initializing Driver

To initialize a driver, write the following syntax.

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

Here, class_name is the driver name you want to invoke.

For example, to invoke a driver class main_class, do the following.

  1. $this->load->driver('main_class');  

To invoke its method,

  1. $this->main_class->a_method();  

And then child classes can be called directly through the parent class, without initializing them.

  1. $this->main_class->first_child->a_method();  
  2.   
  3.     $this->main_class->second_child->a_method();  

Creating Own Drivers

There are three steps to create a driver in CodeIgniter.

  1. Making file structure
  2. Making driver list
  3. Making driver(s)

Making file structure

Go to system/libraries folder of CodeIgniter and make a new folder My_driver. Inside this folder make a file My_driver.php.

Now make a new folder inside My_driver folder, name it as drivers. Inside this new folder make a file, My_driver_first_driver.php.

The following file structure will be shown.

  1. /libraries  
  2.         /My_driver  
  3.             My_driver.php  
  4.             /drivers  
  5. My_driver _first_driver.php  
Codeigniter Driver 1

In CodeIgniter, driver library structure is such that subclasses don?t extend and hence they don't inherit the properties or methods of the main driver (in this case it is My_driver).

  • My_driver - it is a class.
  • My_driver.php - Parent driver
  • My_driver_first_driver.php - Child driver

Making Driver List

In the file My_driver.php in system/libraries/My_driver folder write the following code,

  1. <?php  
  2. defined('BASEPATH') OR exit('No direct script access allowed');  
  3. class CI_My_driver extends CI_Driver_Library  
  4. {  
  5.     function __construct()  
  6.     {  
  7.         $this->valid_drivers = array('first_driver');  
  8.     }  
  9.     function index()  
  10.     {  
  11.         echo "<h1>This is Parent Driver</h1>";  
  12.     }  
  13.      }  

In the file My_driver_first_driver.php in system/libraries/My_driver/drivers write the following code,

  1. <?php  
  2. defined('BASEPATH') OR exit('No direct script access allowed');  
  3. class My_driver_first_driver extends CI_Driver  
  4. {  
  5.     function first()  
  6.     {  
  7.         echo "<h1>This is first Child Driver</h1>";  
  8.     }  
  9.      }  
  10. ?>  

Make a controller file Mydrive.php in application/controllers with the following code,

  1. <?php  
  2. defined('BASEPATH') OR exit('No direct script access allowed');  
  3.     class Mydrive extends CI_Controller  
  4.  {  
  5.     function __construct()  
  6.     {  
  7.              parent::__construct();  
  8.          $this->load->driver('my_driver');  
  9.       }  
  10.     public function invoke1()  
  11.     {  
  12.        $this->my_driver->index();  
  13. }  
  14.     public function invoke2()  
  15.     {       
  16.        $this->my_driver->first_driver->first();  
  17.     }  
  18. }?>  
  19.    

On your browser run the URL, http://localhost/driver/index.php/mydrive/invoke1

Comments

Popular posts from this blog

Codeigniter Pro Level Tips and Tricks

Login with Facebook in CodeIgniter

PHP - File Uploading