使用Codeigniter创建自定义驱动程序 [英] Create custom driver using Codeigniter

查看:53
本文介绍了使用Codeigniter创建自定义驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create my custom driver using 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控制器中测试代码:

Testing code in welcome.php Controller :

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

,但输出为:请求了无效的驱动程序Test_driver.test_driver_first_driver".没有人有任何想法,不幸的是Codeigniter用户指南没有包含用于创建自定义驱动程序的步骤.

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.

推荐答案

其最佳做法,或者我应该说我认为我总是避免在父类中为驾驶员添加下划线所以对我来说文件结构是这样的

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

在控制器中

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

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

即文件夹 testdriver

文件-

testdriver.php ( class testdriver扩展了CI_Driver_Library )

testdriver_first_driver.php ( class testdriver_first_driver扩展了CI_Driver )

希望它会有所帮助.:)

hope it is helpful. :)

这篇关于使用Codeigniter创建自定义驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆