Hi everybody! Here we go .. today by my broken English, so try hard to understand, because we have very interesting topic: How to use Drivers in CodeIgniter?
What are Drivers?
‘Drivers’ is new feature of CodeIgniter 2.0. In official docs of CI 2.0 is written:
Drivers are a special type of Library that has a parent class and any number of potential child classes. Child classes have access to the parent class, but not their siblings. Drivers provide an elegant syntax in your controllers for libraries that benefit from or require being broken down into discrete classes.
My explanation will be similar:
Drivers allow you to make superlibrary, and many small libraries called drivers. That enables you to make more elegant classes (drivers), and more elegant syntax inside your app.
Usage
In CI docs you can find this example:
$this->load->driver('<var>some_parent</var>'); $this->some_parent->some_method(); $this->some_parent->child_one->some_method(); $this->some_parent->child_two->another_method();
Real world example:
$this->load->driver('connect'); $this->connect->facebook->get_friends(); $this->connect->twitter->get_twitts();
I really like idea of this feature. You can box up lot of functionality with it and make one elegant solution.
How to make Driver in CI?
And finaly problem has occured, because there is not written enought in CI to know how to write them. I was googling for long time to find it out, with no answer. That is also a reason, why I posted this short article. So here are 3. steps:
- Making file structure
- Making driver list
- Making driver(s)
I. Making file structure
We’ll start with file structure. On your app/libraries make new folder and call it Test_driver. Inside make new file named Test_driver.php. Now make another folder inside Test_driver folder, and name it drivers. Inside drivers folder make a new file, our first driver, Test_driver_first_driver.php.
You should have this file structure:
/libraries /Test_driver /drivers <strong>Test_driver_first_driver.php</strong> <strong>Test_driver.php</strong>
We’re ready now.
II. Making driver list
It’s time for PHP. Open /Test_driver/Test_driver.php and write this code:
class Test_driver extends <strong>CI_Driver_Library</strong> { function __construct() { <strong>$this->valid_drivers</strong> = array('test_driver_first_driver'); /* Yes, it's really required */ } }
Make sure it extends CI_Driver_Library. When you’re done, let’s make first driver and test it!
III. Making driver(s)
Now open your /Test_driver/drivers/Test_driver_first_driver.php and paste this code:
<?php class Test_driver_first_driver extends <strong>CI_Driver</strong> { function index() { echo "Hello world!"; } } ?>
Make sure it extends CI_Driver. Ok, driver is written let’s test it. Open any controller (i.e. welcome.php) and add:
$this->load->driver('test_driver'); $this->test_driver->first_driver->index();
.. and it should work
Conclusion
This is my first article in blog written in English! Trust me, I can speak Slovak fluently. I hope, this quick tut helps you to understand where and how to write and use Drivers in CodeIgniter. Have a nice day and don’t forget to share this article on FB or something similar
Hi friend, thanks for writing this tutorial. This features is very useful
Nice tutorial, keep up the good work!
Nice article (and good English), cheers!
Thank you
great tutorial. really filled in the gaps in the Code Igniter User Guide article on creating drivers
Working on my first driver.
Seeing Test_driver_first_driver extends ‘CI_Driver’ and not ‘Test_driver’, is there any way to have a common function in ‘Test_driver’ that’s available to ‘Test_driver_first_driver’ and another others I might create?
Thanks for the article!
You may try this in place of mandatory __construct method:
class Test_driver extends CI_Driver_Library {
protected $valid_drivers = array(
‘test_driver_first_driver’
);
}
Thanks for the article, I too was a bit confused after reading the CI docs page.
Don’t worry about your english, I had no problems understanding your article. It’s a lot better than my Slovakian.
Clear and concise, the best CodeIgniter Drivers tutorial; there’s another here but for newbies like me yours is like gold.
Your english is good.
Nice article.
For those who are interested I’ve creating a working example tutorial that shows you a real world application using drivers.
http://www.kevinphillips.co.nz/news/codeigniter-drivers-tutorial
As usual, the webmaster posted correctly..!
I have to get around my own passion to your kindness supporting people who actually need assist with the bradenton area of curiosity. Your own actual commitment to passing the solution around had been good and possess regularly prompted personnel much like me to reach their set goals. Your personal beneficial beneficial data implies this much a person much like me but still far more to be able to my own friends. Thanks a lot from all of folks.
Wow o_0
Neither CI’s official user guide nor the source code tell us that
$this->valid_drivers
in the superlibrary is a must.Thanks, dude. Your tut’s like a godsent to me :0)
Been looking for something like this!!! The official CodeIgniter documentation is dissapointingly sparse….
Your tutorial fills in the gaps!
I just could not depart your web site before suggesting that I really enjoyed the standard info a person provide for your guests?
Is going to be again regularly in order to check up on new
posts
How to use a CI Instance in Test_driver_first_driver ?
codeigniter documentation lacks lot of information. what a shame
not working giving Unable to load the requested class: test_driver error
sorry it was my coding mistake now it is working fine