【问题标题】:Create custom driver using Codeigniter使用 Codeigniter 创建自定义驱动程序
【发布时间】:2017-09-20 18:01:41
【问题描述】:

我正在尝试使用 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');  
     } 
}

驱动子类:

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 用户指南不包含创建自定义驱动程序的步骤。

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    它的最佳实践或者我应该说我的想法 我总是避免在驱动程序的父类中使用下划线 所以对我来说文件结构有点像这样

    /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.php (class testdriver extends CI_Driver_Library)

    testdriver_first_driver.php(class testdriver_first_driver extends CI_Driver)

    希望对您有所帮助。 :)

    【讨论】:

    • 如何自动加载testdriver?
    • @motaz 根据我的说法,到目前为止,驱动程序无法直接自动加载。如果我错了,请纠正我。但我使用父类构造函数来加载它,所以它类似于自动加载本身,例如在My_controller extends CI_controller 的构造函数中,我们加载所需的驱动程序,在A_controller extends My_controller 中,我们可以在任何地方使用它。
    【解决方案2】:

    我尝试了 Karan 的回答,但我删除了 valid_drivers 值中的父母姓名:

    <?php
      class Testdriver extends CI_Driver_Library{
    
    
        function __construct(){
          $this->valid_drivers = array('first_driver');
        }
      }
    ?>
    

    这对我有用,您可能想尝试一下。归功于 Karan。

    【讨论】:

      【解决方案3】:

      我刚刚在 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”的驱动程序类,这当然不存在。

      【讨论】:

        【解决方案4】:

        核心系统库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');
            }
        }  
        

        【讨论】:

          【解决方案5】:

          CodeIgniter SPL 自动加载器

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

          【讨论】:

            猜你喜欢
            • 2010-09-25
            • 1970-01-01
            • 1970-01-01
            • 2013-09-13
            • 1970-01-01
            • 1970-01-01
            • 2016-09-22
            • 2021-05-28
            • 2013-04-08
            相关资源
            最近更新 更多