我正在使用辅助函数来验证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
方法相同的文件中,但它仍然说这是未定义的。
为什么我会收到此错误,即使此功能已明确定义?
答案 0 :(得分: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
数组来自动加载它
答案 1 :(得分: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();
}
}
?>