使用Codeigniter创建自定义驱动程序

时间:2014-05-08 01:01:43

标签: php codeigniter

我正在尝试使用Codeigniter创建自定义驱动程序

文件结构:

/libraries
    /Test_driver
         /drivers
              Test_driver_first_driver.php
         Test_driver.php

司机超级班:

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!"; 
     } 
}

在welcome.php控制器中测试代码:

$this->load->driver('test_driver');
$this->test_driver->test_driver_first_driver->index();

但输出结果是:“无效的驱动程序请求了Test_driver.test_driver_first_driver”。 有没有人知道,遗憾的是Codeigniter用户指南不包含创建自定义驱动程序的步骤。

5 个答案:

答案 0 :(得分:3)

最佳实践或者我应该说我的想法我总是避免在父类中为驱动程序强调 所以对我来说文件结构是这样的

/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!"; 
         } 
    }

在控制器中

$this->load->driver('testdriver');
$this->testdriver->first_driver->index();

注意:即使您不使用ucfirst(),它仍然可以使用

即。文件夹testdriver

档案 -

testdriver.phpclass testdriver extends CI_Driver_Library

testdriver_first_driver.phpclass testdriver_first_driver extends CI_Driver

希望它有所帮助。 :)

答案 1 :(得分:1)

我尝试了Karan的答案,但我在valid_drivers的值中删除了父母的名字:

<?php
  class Testdriver extends CI_Driver_Library{


    function __construct(){
      $this->valid_drivers = array('first_driver');
    }
  }
?>

这对我有用,你可能想尝试一下。致Karan的信用。

答案 2 :(得分:0)

我刚刚在CodeIgniter v2.2.0中解决了这个问题,所以我认为我已经填上了。自定义驱动程序的文档很少,因为示例没有显示完整的设置。现有的核心CodeIgniter驱动程序也没有以一致的方式组织,驱动程序父类文件位于文档所说的应该位于不同的目录位置等等。所以你几乎没有什么可去的,只是查阅核心驱动程序库代码

在您给定的情况下,驱动程序被视为无效是因为您在调用时有效地添加了两次父类名称。这样:

$this->test_driver->test_driver_first_driver->index();

应改为:

$this->test_driver->first_driver->index();

查看驱动程序父类扩展的核心代码:

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;

注意那里的最后一行。基本上,CI正在尝试加载名为“Test_driver_test_driver_first_driver”的驱动程序类,当然这种驱动程序类不存在。

答案 3 :(得分:0)

对于核心系统库driver.php中的codeignaiter 3问题 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');
    }
}  

答案 4 :(得分:0)

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');