【问题标题】:Codeigniter: Extend Custom library from core folder in a libary classCodeigniter:从库类中的核心文件夹扩展自定义库
【发布时间】:2015-02-13 19:57:12
【问题描述】:

我在应用程序/核心文件夹中创建了一个名为 MY_Library 的核心库,我正在尝试从应用程序/库中的库类扩展它,但不幸的是它找不到该文件。

//application/core/My_Library.php
class My_Library{
    function __construct(){

    }

    /**
     * This is the generic function that calls php curl to make a query.
     * @param $url
     * @param array $data
     * @param string $type
     * @return mixed|string
     */
    public function callService($url,$data=array(),$type="get"){
        if (strtolower($type) == "get"){
            $url .= "?".http_build_query($data);
            $response = $this->doGet($url);
        }else if (strtolower($type) == "post"){
            $fields_string = http_build_query($data);
            $response = $this->doPost($url,$fields_string);
        }else{
            $response = "INVALID REQUEST";
        }

        return $response;
    }

}

In my application/libraries
class CakePixel extends MY_Library{
    function __construct(){
        parent::__construct();
    }
    public function fireCakePixel($cakeOfferId,$reqId,$transactionId){
        $cakeUrl = "http://oamtrk.com/p.ashx";
        $param = array(
            "o" =>  $cakeOfferId,
            "reqid" =>$reqId,
            "t"     => $transactionId
        );
        $response = $this->callService($cakeUrl,$param,"get");
    }
}

但是我遇到了一个致命错误

PHP Fatal error:  Class 'MY_Library' not found in /application/libraries/cakeApi/pixel/CakePixel.php on line 10, referer: 

如果可能,我如何在不使用 require_once 或包含类文件的情况下解决此问题。

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    您不应在core 目录中加载库。 core 目录用于核心类或您希望控制器从中扩展的“父”控制器。您应该加载 Codeigniter 的 libraries 目录中的所有库,然后在您的控制器中,您可以像这样调用库中的函数:

    $this->load->library('my_library');
    $results = $this->my_library->callService($params);
    

    【讨论】:

      【解决方案2】:

      如果系统库存在,CI 总是首先查找系统库,然后在应用程序下的核心或库文件夹中查找 MY_,系统 cor 库目录中没有 library.php,这就是您收到此错误的原因。如果您想从核心或库目录自动加载第三方库,您可以使用以下代码,您需要将其添加到底部或顶部的 config.php 中

      spl_autoload_register(function($class)
      {
          if (strpos($class, 'CI_') !== 0)
          {
              if (file_exists($file = APPPATH . 'core/' . $class . '.php'))
              {
                  include $file;
              }
              elseif (file_exists($file = APPPATH . 'libraries/' . $class . '.php'))
              {
                  include $file;
              }
          }
      }); 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多