codeigniter 中的自定义助手

Posted

技术标签:

【中文标题】codeigniter 中的自定义助手【英文标题】:Custom Helpers in codeigniter 【发布时间】:2012-03-28 23:22:00 【问题描述】:

我在 ./application/helpers 中创建了一个自定义助手,但是我收到了这个错误

无法加载请求的文件 helpers/curl_helper

这是我的帮助文件中的代码:

    function send(array $request, $url, $method)

    //Validating if the required extensions are installed or not
    if( !function_exists('json_encode') )   return false;
    if( !function_exists('curl_init') )     return false;

    //Converting the array into required json format
    $request = json_encode($request);

    //Setting header required for requests
    $header[] = "Content-type: application/json";
    $header[] = "Content-length: ".strlen($request) . "\r\n";

    //If the request method is get append the data into requests header
    if( $method == 'GET' or $method == 'get' )      $header[] = $request;

    //Initializing curl
    $ch = curl_init();
    //Setting curl options for request
    curl_setopt( $ch, CURLOPT_URL, $url);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt( $ch, CURLOPT_HTTPHEADER, $header );
    curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, $method );

    //If the request method is post add the data we need to enable post 
    //request and define the data in post fields
    if( $method == 'POST' or $method == 'post' ) 
        curl_setopt( $ch, CURLOPT_POST, 1);
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $request);
    

    //Executing the curl request and storing the result in a variable
    $result = curl_exec( $ch );
    //Closing curl conneciton
    curl_close($ch);

    return json_decode($result);

我正在将它加载到我的库中,例如:

$this->loader =& get_instance();
$this->loader->load->helper('curl');

告诉我哪里做错了?

更新: 当我将函数放在我想使用的同一个库中尝试了太多东西后,我发现行中有错误

curl_setopt( $ch, CURLOPT_HTTPHEADER, $header );

当我评论这条线功能正常。我不知道错误在哪里,请帮助我。据我认为这是加载程序错误的原因。

【问题讨论】:

您是否将您的助手命名为curl_helper.php Offcourse,我已经尝试过任何一种方式,方法是在子类前缀前面加上前缀并删除它。 不带加载器试试看$this->load->helper('curl'); @AntonSementsov 在我删除加载程序时遵循您的建议,我收到以下错误 Undefined property: Video_Api::$load 这个答案***.com/questions/804399/… 可能会有所帮助。 【参考方案1】:

您使用的是 CodeIgniter >= v 2.0.3 吗?检查代码点火器加载器代码,我只能看到帮助加载失败的几种方式:

文件命名不正确。您的文件必须具有 .php 扩展名,即。 curl_helper.php 但您似乎已经检查过了。 文件不在正确的位置(应用程序/帮助程序)。同样,您似乎已经检查过了。 正在运行的 Web 服务器无法访问文件。权限问题?

子类前缀:你的配置设置是什么 子类前缀?这是默认设置:

$config['subclass_prefix'] = 'MY_';

CI 允许用户通过添加前缀来覆盖他们的默认助手。例如,您可以通过一个名为 MY_array_helper.php 的文件来覆盖数组助手。如果碰巧您的帮助程序与子类前缀匹配,那么 CI 假定您正在尝试覆盖系统帮助程序并尝试确保该帮助程序存在于系统帮助程序目录中。例如,如果您在 application/helpers/MY_curl_helper.php 中有一个助手,那么 CI 会检查 system/helper/curl_helper.php 中是否存在一个助手。换句话说,确保你的助手文件不匹配子类前缀。

【讨论】:

你是如何调用 send() 函数的?我可以看到的一件事可能是一个问题,当函数使用 GET 时,它会将 JSON 字符串附加到 $request,但这会导致 HTTP 标头无效【参考方案2】:

好吧,如果您使用的是"MY_helpername",那么请检查您是否将服务器上的文件命名为“my_helpername”,我不知道原因,但 CI 会在 helpers 文件夹中查找 my_helpername.php,但是不是MY_helpername。我过去遇到过这个问题,将我的 linux 机器上的名称从 MY_helpername 更改为 my_helpername 对我有用。

希望对你有帮助

【讨论】:

是的,我已经这样做了,不知道是否符合标准,但我终于找到了这个解决方案。【参考方案3】:

解决方案:对我来说,将文件重命名为小写即可。

【讨论】:

还有另一个答案(实际上是已接受的答案)与您刚才所说的相同。

以上是关于codeigniter 中的自定义助手的主要内容,如果未能解决你的问题,请参考以下文章

创建自定义助手类?

Laravel 加载并初始化自定义助手类

Laravel Blade - 自定义助手

资源(resx)自定义助手

如何在车把 ember 中定义自定义助手

jQuery UI Draggable 的自定义助手