在控制器中找不到帮助程序中定义的函数


function defined in helper not found in controller

我正在使用一个辅助函数来验证Codeigniter中的XML。

我的帮助程序函数在 xml_validation_helper.php 中定义,如下所示:

/**
 * Function to generate a short html snippet indicating success
 * or failure of XML loading
 * @param type $xmlFile
 */  
function validate_xml($xmlFile){
    libxml_use_internal_errors(true);
    $dom = new DOMDocument();
    $dom->validateOnParse = true;
    $dom->load($xmlFile);
    if (!$dom->validate())
    {
        $result = '<div class="alert alert-danger"><ul>';
        foreach(libxml_get_errors() as $error)
        {
            $result.="<li>".$error->message."</li>";
        }
        libxml_clear_errors();
        $result.="</ul></div>";
    }
    else 
    {
        $result = "<div class='alert alert-success'>XML Valid against DTD</div>";
    }
    return $result;
}

我在我的控制器中使用它(特别是在index方法中),如下所示:

 function index() {
    $this->data['pagebody'] = "show_trends";
    $this->load->helper("xml_validation");
    $this->data['pokedex'] = display_file(DATA_FOLDER ."/xml/pokedex.xml");
    $pokedexResult = validate_xml($this->data['pokedex']);
    $this->data['gameSales'] = display_file(DATA_FOLDER . "/xml/sales.xml");
    $gameSalesResult = validate_xml($this->data['gameSales']);
    $this->render();
}

但是,即使我可以清楚地加载文件,我也不断收到" Fatal error: Call to undefined function validate_xml() in C:'xampp'htdocs'project'application'controllers'show_trends.php on line 15错误。我什至尝试将该函数移动到与 index 方法相同的文件中,但它仍然说它是未定义的。

为什么我会收到此错误,即使此功能已明确定义?

如果您的帮助程序名为 the_helper_name_helper.php(它必须以 _helper.php 结尾)并且位于application/helpers中,您必须使用以下方法加载帮助程序文件:

$this->load->helper('the_helper_name')

如果您计划经常在此帮助程序中使用函数,则最好通过在 application/config/autoload.php 中向 $config['helpers'] 数组添加 'the_helper_name' 来自动加载它

You must load libraries and helper files in contructor function
check it out
<?PHP
class controllername extends CI_Controller
{
public function __construct()
{
    $this->load->helper("xml_validation");
}
public function index() {
    $this->data['pagebody'] = "show_trends";
   // $this->load->helper("xml_validation");
    $this->data['pokedex'] = display_file(DATA_FOLDER ."/xml/pokedex.xml");
    $pokedexResult = validate_xml($this->data['pokedex']);
    $this->data['gameSales'] = display_file(DATA_FOLDER . "/xml/sales.xml");
    $gameSalesResult = validate_xml($this->data['gameSales']);
    $this->render();
}
}
?>