in autoload.php
public $helpers = [
'general'
];
helper is loaded and helper file defined 2 functions
<?php
function datetimeformat($date)
{
return date("Y-m-d H:i:s", strtotime($date));
}
function randomnumber()
{
return "111";
}
and in controller i called the helper but is not working
<?php
namespace AppControllers;
/**
* Rating Controller For Admin
*/
class Rating extends BaseController
{
public function index()
{
// Load either general_helper or general
if ( helper('general_helper') || helper('general')) {
echo "test";exit();
}
}
}
in this code helper is not loaded i cant find what is the issue please any one have idea
2
Answers
The helper() function does not return a value, so don’t try to assign it to a variable.
So your conditional check will never evaluate as
true
.After you’ve loaded your helpers, just check if the desired function exists using
exit(function_exists('randomnumber') ? 'exists' : 'nope');
.See also:
This line is useless
if ( helper('general_helper') || helper('general'))
You do not have to check whether the helper has been loaded.
It should be loaded, since you did it within the
autoload
.If it’s not loaded, then:
Verify that the name of the helper
general_helper
corresponds to the name of the file. It must beHelpersgeneral_helper.php
Call
public $helpers = ['general_helper'];
in theautoload
.This is for CI4, as described in the official documentation.