0

我刚刚将我的项目从 CodeIgniter 2 升级到 CodeIgniter 3 以获得驱动程序自动加载功能。我正在尝试创建一个自定义驱动程序,但遗憾的是文档没有给我执行它的步骤。

我的驱动程序在 CodeIgniter 3 中运行良好,但在 2 中却不行。我已经根据文档更新了所有类名和文件名。

我有以下文件结构:

/libraries
    /Testdriver
         /drivers
             Testdriver_test.php
         Testdriver.php

Testdriver.php 的内容:

class Testdriver extends CI_Driver_Library
{
     function __construct()
     {
          $this->valid_drivers = array('testdriver_test'); //Still not sure why this must be here, but the documentation doesn't explain me anything
     }

     function test()
     {
          echo "Hello world from parent driver";
     }
}

Testdriver_test.php 的内容:

class Testdriver_test extends CI_Driver
{
     public function index()
     {
         echo "Hello world!";
     }
 }

自动加载驱动,调用 pages.php 中的函数:

  $this->testdriver->test(); //This works, I can successfully call the method from the parent driver
  $this->testdriver->test->index(); //This doesn't work, gives me the "Invalid driver requested" error

为什么这在 CodeIgniter 2 中有效而不再有效?我该如何解决?

4

1 回答 1

1

您所有的文件名和类名都可以。一个错误在 $valid_drivers 数组中。你有这个:

$this->valid_drivers = array('testdriver_test');

你应该输入这个:

$this->valid_drivers = array('test'); 
于 2015-02-24T22:37:14.990 回答