Reputation: 625
I'm using a helper function to validate XML in Codeigniter.
My helper function is defined in xml_validation_helper.php
and is as follows:
/**
* 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;
}
I'm using it in my controller (specifically in the index
method) and that is as follows:
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();
}
However, I keep getting a "Fatal error: Call to undefined function validate_xml() in C:\xampp\htdocs\project\application\controllers\show_trends.php on line 15
error, even though I can clearly load the file. I've even tried to move the function into the same file as the index
method, but it still says it's undefined.
Why am I getting this error, even though this function is clearly defined?
Upvotes: 1
Views: 566
Reputation: 6759
Provided your helper is named the_helper_name_helper.php (it must end with _helper.php) and is located in the application/helpers
you have to load the helper file using:
$this->load->helper('the_helper_name')
If you plan on using functions in this helper often, you better autoload it by adding 'the_helper_name'
to the $config['helpers']
array in application/config/autoload.php
Upvotes: 2
Reputation: 152
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();
}
}
?>
Upvotes: 1