Codeigniter:从库中的核心文件夹扩展自定义库 class
Codeigniter: Extend Custom library from core folder in a libary class
我在 application/core 文件夹中创建了一个名为 MY_Library 的核心库,我试图从 application/libraries 中的库 class 扩展它,但不幸的是它找不到文件。
//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 的情况下解决这个问题,或者如果可能的话从 class 文件中包含。
您不应加载 core
目录中的库。 core
目录用于核心 类 或您希望控制器从中扩展的 "parent" 控制器。您应该在 Codeigniter 的 libraries
目录中加载所有库,然后,在您的控制器中,您可以像这样调用库中的函数:
$this->load->library('my_library');
$results = $this->my_library->callService($params);
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;
}
}
});
我在 application/core 文件夹中创建了一个名为 MY_Library 的核心库,我试图从 application/libraries 中的库 class 扩展它,但不幸的是它找不到文件。
//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 的情况下解决这个问题,或者如果可能的话从 class 文件中包含。
您不应加载 core
目录中的库。 core
目录用于核心 类 或您希望控制器从中扩展的 "parent" 控制器。您应该在 Codeigniter 的 libraries
目录中加载所有库,然后,在您的控制器中,您可以像这样调用库中的函数:
$this->load->library('my_library');
$results = $this->my_library->callService($params);
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;
}
}
});