Codeigniter: Extend Custom library from core folder in a libary class

纵然是瞬间 提交于 2019-12-25 04:22:59

问题


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.


回答1:


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);



回答2:


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;
        }
    }
}); 


来源:https://stackoverflow.com/questions/28507742/codeigniter-extend-custom-library-from-core-folder-in-a-libary-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!