2013-10-05 33 views
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方法相同的文件中,但它仍然表示它是未定義的。

爲什麼我得到這個錯誤,即使這個函數是明確定義的?

回答

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(); 
} 
} 

?> 
1

提供你的助手被命名爲the_helper_name_helper.php(它必須與_helper.php結尾)和位於application/helpers您在使用加載幫助文件:

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

如果你打算經常使用此幫手中的函數,您最好通過將'the_helper_name'添加到$config['helpers']陣列中來自動加載它application/config/autoload.php

相關問題