Codeigniter:从libary类中的核心文件夹扩展自定义库 [英] Codeigniter: Extend Custom library from core folder in a libary class

查看:172
本文介绍了Codeigniter:从libary类中的核心文件夹扩展自定义库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

但我遇到致命错误

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.

推荐答案

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

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

这篇关于Codeigniter:从libary类中的核心文件夹扩展自定义库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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