2

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'];
Senthil
  • 757
  • 2
  • 7
  • 25
  • 1
    You autoload code is incorrect. Try to use like $autoload['helped'] = array('functions'); –  Nov 05 '19 at 11:05

5 Answers5

2

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'];
    //...
}
IVIike
  • 87
  • 8
1

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

Senthil
  • 757
  • 2
  • 7
  • 25
Tsefo
  • 409
  • 4
  • 15
  • 1
    I got the error `Call to undefined function chicking_helper()` – Senthil Nov 06 '19 at 07:45
  • Try protected $helpers = ['url', 'file', 'MY_helper']; exactly like your filename is, may be capital letters are important – Tsefo Nov 06 '19 at 09:02
1

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',];
.....
}
Arnes He
  • 87
  • 5
1

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.

-1

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.

  • I added the line in `autoload.php`. but not working. Any other possibilities – Senthil Nov 05 '19 at 11:37
  • did you create the helper function in system/helpers or application/helpers directory? Please read this doc https://codeigniter.com/user_guide/general/helpers.html –  Nov 05 '19 at 11:44
  • Yes I created the files `app/Helpers/My_helper.php`. – Senthil Nov 05 '19 at 12:20