Search code examples
phpcodeignitercodeigniter-3

getting error when I try to use Drivers in CodeIgniter


I'm trying to create a custom driver in codeigniter, but i got error.

here is the File hierarchy:

 /libraries
    /libraries/Taker
        /libraries/Taker/Taker.php
        /libraries/Taker/drivers
            /libraries/Taker/drivers/Taker_test.php

Code in libraries/Taker/Taker.php:

   class Taker extends CI_Driver_Library  {
        function hello() {
            echo 123;
        }
    }

Code in libraries/Taker/drivers/Taker_test.php:

class Taker_test extends CI_Driver {
    function world() {
        echo 123;
    }
}

The code in the controller:

$this->load->driver('taker');
$this->taker->hello(); //work
$this->taker->test->world(); //eror here

the error i got in the controller:

Invalid driver requested: Taker_test

Any Suggestions? Thanks


Solution

  • The solution is that I had to use $this->valid_drivers:

    class Taker extends CI_Driver_Library  {
        
        function __construct() {
              $this->valid_drivers = ['test'];
         }
        
        
        function hello() {
            echo 123;
        }
    }