简体   繁体   中英

CodeIgniter 4, helper function not working

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'];

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

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: eg: 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',];
.....
}

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.

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.

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'];
    //...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM