Reputation: 445
I'm trying to create my custom driver using Codeigniter
Files structure:
/libraries
/Test_driver
/drivers
Test_driver_first_driver.php
Test_driver.php
Driver super class:
class Test_driver extends CI_Driver_Library
{
function __construct()
{
$this->valid_drivers = array('test_driver_first_driver');
}
}
Driver Subclass :
class Test_driver_first_driver extends CI_Driver
{
function index()
{
echo "Hello world!";
}
}
Testing code in welcome.php Controller :
$this->load->driver('test_driver');
$this->test_driver->test_driver_first_driver->index();
but the output was : "Invalid driver requested Test_driver.test_driver_first_driver". Does any one have any idea, Unfortunately Codeigniter user guide does not contains steps for creating custom driver.
Upvotes: 1
Views: 2489
Reputation: 1
CodeIgniter SPL Autoloader
/*
|--------------------------------------------------------------------------
| Autoloader function
|--------------------------------------------------------------------------
|
| Add to the bottom of your ./application/config/config.php file.
|
| @author Brendan Rehman
| @param $class_name
| @return void
*/
function __autoloader($class_name)
{
// class directories
$directories = array(
APPPATH . 'core/',
// add more autoloading folders here� and you�re done.
);
// for each directory
foreach ($directories as $directory)
{
// see if the file exsists
if (file_exists($directory.$class_name.'.php'))
{
require_once($directory.$class_name.'.php');
// only require the class once, so quit after to save effort (if
you got more, then name them something else
return;
}
}
}
spl_autoload_register('__autoloader');
Upvotes: 0
Reputation: 41
I tried Karan's answer but I removed the parent's name in valid_drivers' value:
<?php
class Testdriver extends CI_Driver_Library{
function __construct(){
$this->valid_drivers = array('first_driver');
}
}
?>
This worked for me, you might want to give it a try. Credits to Karan.
Upvotes: 1
Reputation: 11
For codeignaiter 3 problem in core system libraries driver.php
testdriver_first_driver.php
class Testdriver_first_driver extends CI_Driver {
public function index()
{
echo "Hello world!";
}
}
testdriver.php
class Testdriver extends CI_Driver_Library{
function __construct(){
$this->valid_drivers = array('first_driver');
}
}
Upvotes: 0
Reputation: 1492
I have just grappled with this in CodeIgniter v2.2.0 so thought I'd chip in. The scant documentation on custom drivers isn't too helpful as the example does not show the complete setup. The existing core CodeIgniter drivers are not organised in a consistent way either, with the driver parent class files being in different directory locations to where the docs say they should be etc. so you have little to go by but to consult the core Driver Library code.
In your given situation, the driver is seen as invalid is because you are effectively adding on the parent class name twice when calling it. This:
$this->test_driver->test_driver_first_driver->index();
Should be changed to:
$this->test_driver->first_driver->index();
Looking at the core code that the driver parent class extends:
class CI_Driver_Library {
protected $valid_drivers = array();
protected $lib_name;
// The first time a child is used it won't exist, so we instantiate it
// subsequents calls will go straight to the proper child.
function __get($child) {
if (!isset($this->lib_name)) {
$this->lib_name = get_class($this);
}
// The class will be prefixed with the parent lib
$child_class = $this->lib_name . '_' . $child;
Note the last line there. Basically, CI was trying to load a driver class named "Test_driver_test_driver_first_driver", which of course didn't exist.
Upvotes: 0
Reputation: 1467
its best practice or i should say my thinking that i always avoid underscores in parent class for the driver so for me the file structure is some what like this
/libraries
/Testdriver
/drivers
Testdriver_first_driver.php
Testdriver.php
Testdriver.php
<?php
class Testdriver extends CI_Driver_Library
{
function __construct()
{
$this->valid_drivers = array('testdriver_first_driver');
}
}
Testdriver_first_driver.php
<?php
class Testdriver_first_driver extends CI_Driver
{
public function index()
{
echo "Hello world!";
}
}
In controller
$this->load->driver('testdriver');
$this->testdriver->first_driver->index();
Note : even if you don't use ucfirst() it will still work
i.e. Folder testdriver
Files -
testdriver.php
(class testdriver extends CI_Driver_Library
)
and
testdriver_first_driver.php
(class testdriver_first_driver extends CI_Driver
)
hope it is helpful. :)
Upvotes: 3