Reputation: 1462
I have made a core library in application/core folder called MY_Library and I am trying to extend it from library class in application/libraries but unfortunately it cant find the file.
//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");
}
}
But I am getting a fatal error
PHP Fatal error: Class 'MY_Library' not found in /application/libraries/cakeApi/pixel/CakePixel.php on line 10, referer:
How can i resolve this without using require_once or include from class file if possible.
Upvotes: 0
Views: 3837
Reputation: 4574
CI always fist look for system libraries if they exists then it look for MY_ in core or libraries folder under application, there is no library.php in system cor library directory that's why you are getting this error. if you want to autoload third party libraries from core or library directory you can use the following code you need to add this in config.php at bottom or top
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;
}
}
});
Upvotes: 0
Reputation: 12142
You should not load libraries in the core
directory. The core
directory are for core classes or for "parent" controllers that you want your controllers to extend from. You should load all libraries in the libraries
directory of Codeigniter, then, in your controller, you can call functions in your library like this:
$this->load->library('my_library');
$results = $this->my_library->callService($params);
Upvotes: 2