1

我正在使用辅助函数来验证 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方法相同的文件中,但它仍然说它是未定义的。

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

4

2 回答 2

2

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

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

如果你打算经常在这个助手中使用函数,你最好通过添加'the_helper_name'$config['helpers']数组中来自动加载它application/config/autoload.php

于 2013-10-06T01:47:14.657 回答
1
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();
}
}

?>
于 2013-10-05T05:19:08.997 回答