CodeIgniter Driver
Drivers are introduced in CodeIgniter 2.0 and onwards.
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.
$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.
$this->load->driver('main_class');
To invoke its method,
$this->main_class->a_method();
And then child classes can be called directly through the parent class, without initializing them.
$this->main_class->first_child->a_method();
$this->main_class->second_child->a_method();
Creating Own Drivers
There are three steps to create a driver in CodeIgniter.
- Making file structure
- Making driver list
- 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.
/libraries
/My_driver
My_driver.php
/drivers
My_driver _first_driver.php
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,
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class CI_My_driver extends CI_Driver_Library
{
function __construct()
{
$this->valid_drivers = array('first_driver');
}
function index()
{
echo "<h1>This is Parent Driver</h1>";
}
}
In the file My_driver_first_driver.php in system/libraries/My_driver/drivers write the following code,
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class My_driver_first_driver extends CI_Driver
{
function first()
{
echo "<h1>This is first Child Driver</h1>";
}
}
?>
Make a controller file Mydrive.php in application/controllers with the following code,
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Mydrive extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->driver('my_driver');
}
public function invoke1()
{
$this->my_driver->index();
}
public function invoke2()
{
$this->my_driver->first_driver->first();
}
}?>
On your browser run the URL, http://localhost/driver/index.php/mydrive/invoke1
Look at the above snapshot, the parent driver class in invoked with the function invoke1.
Now run the URL, http://localhost/driver/index.php/mydrive/invoke2
Look at the above snapshot, the child driver class in invoked with the function invoke2.