Php 在控制器中找不到助手中定义的函数
我正在使用一个helper函数来验证Codeigniter中的XML My helper函数在Php 在控制器中找不到助手中定义的函数,php,codeigniter,codeigniter-helpers,Php,Codeigniter,Codeigniter Helpers,我正在使用一个helper函数来验证Codeigniter中的XML My helper函数在xml\u validation\u helper.php中定义,如下所示: /** * Function to generate a short html snippet indicating success * or failure of XML loading * @param type $xmlFile */ function validate_xml($xmlFile){
xml\u validation\u 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;
}
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();
}
然而,我不断得到一个“致命错误:调用未定义的函数validate_xml()在C:\xampp\htdocs\project\application\controllers\show_trends.php的第15行
错误,尽管我可以清楚地加载该文件。我甚至尝试将函数移动到与index
方法相同的文件中,但它仍然说它未定义
为什么我会遇到这个错误,即使这个函数定义得很清楚?您必须在constructor函数中加载库和助手文件
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();
}
}
?>
过来看
数据['pagebody']=“显示趋势”;
//$this->load->helper(“xml_验证”);
$this->data['pokedex']=显示文件(数据文件夹“/xml/pokedex.xml”);
$pokedexResult=validate_xml($this->data['pokedex']);
$this->data['gameSales']=display_文件(data_文件夹“/xml/sales.xml”);
$gameSalesResult=validate_xml($this->data['gameSales']);
$this->render();
}
}
?>
如果您的助手名为\u helper\u name\u helper.php(必须以\u helper.php结尾),并且位于应用程序/helpers
中,则必须使用以下方法加载助手文件:
$this->load->helper('the_helper_name')
如果您计划经常使用此帮助程序中的函数,最好通过在application/config/autoload.php中的$config['helpers']
数组中添加'helper\u name'
来自动加载它