📜 ⬆️ ⬇️

Using drivers in CodeIgniter

Recently, looking through the CodeIgniter User Guide, I accidentally discovered new items for myself about drivers and their creation. Since there is little written in them, I decided to consider this topic in more detail.

So a driver is a special type of library that consists of one parent class and several children. Child classes have access only to the parent class, but do not have access to their fellows. The term “child” does not actually inherit the parent's fields, but only gets access to them.

Drivers are used to split your libraries into separate classes and in CodeIgniter represent the implementation of a structural design pattern Decorator.

')
Suppose we need to create a Connect library to connect to various social networks and retrieve some data from them.

The documentation provides such an example of use:
$this->load->driver('some_parent'); $this->some_parent->some_method(); $this->some_parent->child_one->some_method(); $this->some_parent->child_two->another_method(); 

In real code, it might look like this:
 $this->load->driver('connect'); $this->connect->facebook->get_friends(); $this->connect->twitter->get_twitts(); 

This method allows you to conveniently include many functions in one library with a visual structure.

Driver Creation


The first thing to do when creating a driver is to create a file structure.
In the folder where the libraries are stored, for each driver you need to create a separate directory with the name of the driver. It should contain a parent class and a drivers folder with children. File names must be in lower case letters and begin with a capital letter.

This is how the organization of the driver files looks like:


After the files are created, you can start writing the code. It is important to remember that the parent class of your driver must extend CI_Driver_Library , and all children CI_Driver .

Example of parent driver class:
application / libraries / Connect / Connect.php
 <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class Connect extends CI_Driver_Library { public $valid_drivers; public $CI; function __construct() { $this->CI = & get_instance(); $this->CI->config->load('connect', TRUE); $this->valid_drivers = $this->CI->config->item('modules', 'connect'); } public function get_friends() { return $this->twitter->get_friends() . $this->facebook->get_friends(); } } 

A mandatory point is the $ valid_drivers variable, which is an array with the names of the child classes.
The variable can be declared in the constructor or put into the configuration file. To ensure better portability, it is recommended to use a configuration file.

application / config / connect.php
 <?php $config['modules'] = array('connect_twitter', 'connect_facebook'); 

As in conventional libraries, a reference to the CodeIgniter object can be obtained using the get_instance function.

Create two child classes:
application / libraries / Connect / drivers / Connect_twitter.php
 <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class Connect_twitter extends CI_Driver { public function get_twitts() { return '  :'; } public function get_friends() { return '@vanya, @stepa '; } } 


application / libraries / Connect / drivers / Connect_facebook.php
 <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class Connect_facebook extends CI_Driver { public function get_friends() { return 'Ivan, Stepan '; } } 

Use in controller


The parent class has access to all children, and children only to the parent. Therefore, to exchange data between child classes, you must use the parent class as an intermediate class.

..application / controller / home.php
 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Home extends CI_Controller { public function __construct() { parent::__construct(); $this->load->driver('connect'); } public function friends() { echo '    : '; echo $this->connect->get_friends(); //       } public function twitts() { echo $this->connect->twitter->get_twitts(); //     } } 


The given example is of course very simple, but it shows how to create convenient libraries that can be used later in other projects.

Source: https://habr.com/ru/post/132494/


All Articles