Reputation: 777
I've tried to autoload the helper function. I've added the helper file in autoload.php and called the function in the view file, but it's not working.
app/Config/autoload.php
$psr4 = ['Config' => APPPATH . 'Config',
APP_NAMESPACE => APPPATH,
'App' => APPPATH,
'Helpers' => APPPATH . 'Helpers/MY_helper'];
app/Helpers/My_helper.php
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
function chicking_helper(){
return 'welcome to helper function';
}
app/Views/welcome_message.php
<h1>Welcome to CodeIgniter </h1>
<p class="version">version <?= CodeIgniter\CodeIgniter::CI_VERSION ?></p>
<?php
chicking_helper();
?>
app/Controllers/BaseController
class BaseController extends Controller
{
/**
* An array of helpers to be loaded automatically upon
* class instantiation. These helpers will be available
* to all other controllers that extend BaseController.
*
* @var array
*/
protected $helpers = ['url', 'file'];
Upvotes: 2
Views: 11693
Reputation: 11
You don't need to add the word '_helper' when declaring a new helper in BaseController.php. For instance:
class BaseController extends Controller{
protected $helpers = ['url', 'file', 'my'];
will be looking to load a file named "my_helper.php" in app\Helpers.
Upvotes: 1
Reputation: 87
I solved like this: On your app/Config/autoload.php autoload array optional include the helper source. For attention every _helper is a suffix of each helper filemane. So on app/Controllers/BaseController just add your prefix helper name: e.g: chicking_helper.php
class BaseController extends Controller
{
/**
* An array of helpers to be loaded automatically upon
* class instantiation. These helpers will be available
* to all other controllers that extend BaseController.
*
* @var array
*/
protected $helpers = ['form', 'html', 'chicking',];
.....
}
Upvotes: 1
Reputation: 117
Also make sure the PHP file has the correct naming. I had this problem also because my custom helper file was named "CustomHelper.php" under app/Helpers. After renaming it to "Custom_helper.php" it was possible to load it via the BaseController as mentioned before:
class BaseController extends Controller{
//...
protected $helpers = ['form', 'url','custom'];
//...
}
Upvotes: 2
Reputation: 409
In your BaseController - $helpers array, add an element with your helper filename. Say you have it as app/Helpers/My_helper.php
, then you edit your BaseController like this:
class BaseController extends Controller
{
/**
* An array of helpers to be loaded automatically upon
* class instantiation. These helpers will be available
* to all other controllers that extend BaseController.
*
* @var array
*/
protected $helpers = ['url', 'file', 'my_helper'];
You do not need to touch the Autoload class in your scenario. It is used to map all the different namespaces in your project.
app/Helpers/My_helper.php
if(!function_exists('chicking_helper')) {
function chicking_helper(){
return 'welcome to helper function';
}
}
Codeigniter 4 documentation helpers
Upvotes: 1
Reputation:
Seems you are wrongly declared your helped functions in autload.php
.
It must be declare something like this:
$autoload['helper'] = array('file', 'url');
then in your controller or model, just call the helper function such as default_method($parameter)
. The default_method is your helper function name.
Upvotes: -1