【问题标题】:Undefined function when calling extended helper function in Codeigniter在 Codeigniter 中调用扩展辅助函数时未定义的函数
【发布时间】:2019-11-25 16:32:50
【问题描述】:

我在application/helpers/ 中创建MY_form_helper.php,扩展form_helper.php。函数是这样的:

function alert($message,$type = 'success'){
    $data = array(
        'type' => $type,
        'text' => $message
    );
    return json_encode($data);
}

在我的控制器中,我这样调用函数:

$this->load->helper(array('form', 'url'));
echo alert('Wrong email or password');

但它给了我一个错误

Message: Call to undefined function alert()

那么我在这里遗漏了什么或做错了什么?

【问题讨论】:

    标签: php codeigniter codeigniter-3


    【解决方案1】:

    它应该可以正常工作,因为如果您将其存储在正确的目录中,即 application/helpers 名称为 MY_form_helper.php 现在您应该添加核心助手的扩展:

    $this->load->helper('form');
    var_dump(alert('Wrong email or password'));
    

    测试

    【讨论】:

      【解决方案2】:

      将您的 MY_form_helper.php 助手放置到 application/helper

      <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
      
         function alert($message,$type = 'success'){
            $data = array(
              'type' => $type,
              'text' => $message
         );
         return json_encode($data);
      }
      

      并在您的控制器中放置此代码。

      public function sample()
      {
          $this->load->helper('MY_form_helper'); //load your helper
      
          echo alert('Wrong email or password'); //call your function
      }
      

      让我知道结果。

      【讨论】:

      • 我做了,但仍然出错。现在它说无法加载请求的文件:helpers/my_form_helper.php
      • 您是否将 MY_form_helper.php 助手保存到 application/helper 中?
      • 应该是应用程序/助手吧?是的,我已经放在那个文件夹里了。
      • 没错,然后你加载了这个? $this->load->helper('MY_form_helper');到你的控制器?
      • 是的,我已经尝试过了,但它给了我其他错误,说它没有找到。因为如果我这样做,它会在 system/helpers/ 上查看我的文件,对吗?至少是错误所说的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-24
      • 2020-09-22
      • 2014-01-23
      • 1970-01-01
      • 1970-01-01
      • 2016-11-18
      相关资源
      最近更新 更多