CodeIgniter Forums
my controller fails to find helper function - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: my controller fails to find helper function (/showthread.php?tid=92865)



my controller fails to find helper function - PaulC - 05-07-2025

Hi Team,
I love the new error interface - great job!
Its helping me to upgrade my ci3 to ci 4.6.0
Latest snag is my controller which extends BaseController cannot find the validation function:
- the function is 'is_valid_level' which is defined in a Helper myvalidate_helper.php
- the myvalidate helper is loaded (I think) by adding to the $helpers array in the BaseController (I can see it in the list of files).
- ci3 call was
PHP Code:
if (!is_valid_level($level3)) { .... 
- and I changed it to after a bit of research
PHP Code:
if (!myvalidate(is_valid_level($level3))) { .... 
- and the error reported is: Call to undefined function App\Controllers\myvalidate()
Clearly I have misunderstood something important, so could someone explain what I have got wrong please?
Thx Paul


RE: my controller fails to find helper function - grimpirate - 05-07-2025

Your  helper is called myvalidate_helper, but the function you define inside it (I'm assuming) is named is_valid_level. There is no myvalidate function. There was no need to change your ci3 code. If you want to be sure the helper is being loaded do the following:
PHP Code:
helper('myvalidate');
if (!
is_valid_level($level3)) { ... 
However, as you stated if you did this then you don't need to use the helper function to load the helper as the Controller will auto load it for you.


RE: my controller fails to find helper function - PaulC - 05-07-2025

(Yesterday, 07:25 AM)grimpirate Wrote: Your  helper is called myvalidate_helper, but the function you define inside it (I'm assuming) is named is_valid_level. There is no myvalidate function. There was no need to change your ci3 code. If you want to be sure the helper is being loaded do the following:
PHP Code:
helper('myvalidate');
if (!
is_valid_level($level3)) { ... 
However, as you stated if you did this then you don't need to use the helper function to load the helper as the Controller will auto load it for you.


Thx for reply. I have reverted to the original code which tries to run a function called is_valid_level in the helper myvalidate.
The error is very similar:   Call to undefined function App\Controllers\is_valid_level()
I then tried calling the helper just before the statement and it made no difference.
So, I have checked the BaseController array is correct, and my Controller extends BaseController, so it doesn't appear to be working?
Any other things to check please?
TIA Paul


RE: my controller fails to find helper function - paulbalandan - 05-07-2025

Where did you save the myvalidate_helper.php file? Make sure it is on app/Helpers/ directory.


RE: my controller fails to find helper function - grimpirate - 05-07-2025

Post BaseController.php and myvalidate_helper.php source. Is myvalidate_helper.php located in app/Helpers/myvalidate_helper.php?


RE: my controller fails to find helper function - PaulC - 05-07-2025

PHP Code:
<?php
namespace App\Controllers;
use 
CodeIgniter\Controller;
use 
CodeIgniter\HTTP\CLIRequest;
use 
CodeIgniter\HTTP\IncomingRequest;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
Psr\Log\LoggerInterface;
/**
* Class BaseController
*
* BaseController provides a convenient place for loading components
* and performing functions that are needed by all your controllers.
* Extend this class in any new controllers:
*    class Home extends BaseController
*
* For security be sure to declare any new methods as protected or private.
*/
abstract class BaseController extends Controller
{
    /**
    * Instance of the main Request object.
    *
    * @var CLIRequest|IncomingRequest
    */
    protected $request;
    /**
    * An array of helpers to be loaded automatically upon
    * class instantiation. These helpers will be available
    * to all other controllers that extend BaseController.
    *
    * @var list<string>
    */
    protected $helpers = ['url''form''functions''time''myvalidate''debug'];
    /**
    * Be sure to declare properties for any property fetch you initialized.
    * The creation of dynamic property is deprecated in PHP 8.2.
    */
    protected $session;
protected 
$db;
protected 
$smail;
protected 
$ajax;
protected 
$template;
protected 
$get;
protected 
$make;
protected 
$delete;
    /**
    * @return void
    */
    public function initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request$response$logger);
        // Preload any models, libraries, etc, here.
        $this->session = \Config\Services::session();
$this->db = \Config\Database::connect();
$this->smail = new \App\Libraries\Smail();
$this->ajax = new \App\Libraries\Ajax();
$this->template = new \App\Libraries\Template();
$this->get = new \App\Libraries\Get();
$this->make = new \App\Libraries\Make();
$this->delete = new \App\Libraries\Delete();
    }
}

ls -l ci460/app/Helpers
total 40
-rw-r----- 1 paul www-data  2710 May  7 09:08 debug_helper.php
-rw-r----- 1 paul www-data 20140 May  7 09:37 functions_helper.php
-rw-r----- 1 paul www-data  2363 May  7 09:23 myvalidate_helper.php
-rw-r----- 1 paul www-data  3599 Apr  7 18:53 pagination_helper.php
-rw-r----- 1 paul www-data  1402 Apr  7 18:54 tag_helper.php
-rw-r----- 1 paul www-data  2742 Apr  7 18:54 time_helper.php

<?php

namespace App\Helpers;

// ------------------------------------------------------------------------
if (!function_exists('is_valid_reference')) {
    function is_valid_reference($reference$split ',')
    {
        //Validate reference
        $parts explode($split$reference2);
        $col = @(int) $parts[0];
        $row = @(int) $parts[1];

        return !(
            !is_numeric($col) || strpos($col'.') !== false || $col || $col 1000
        
||
        !is_numeric($row) || strpos($row'.') !== false || $row || $row 1000
        
);
    }
}

// ------------------------------------------------------------------------
if (!function_exists('is_valid_level')) {
    function is_valid_level($input)
    {

        //Validate level
        $separator strpos($input'-') !== false '-' '_';
        list($col$row) = explode($separator$input);

        return !(
            !is_numeric($col) || strpos($col'.') !== false || $col || $col 5
        
||
        !is_numeric($row) || strpos($row'.') !== false || $row || $row 5
        
);
    }
}

// ------------------------------------------------------------------------
if (!function_exists('is_valid_colour')) {
    function is_valid_colour($input)
    {
$input ltrim($input'#');
return 
ctype_xdigit($input);
    }
}

// ------------------------------------------------------------------------
if (!function_exists('valid_twitter_username')) {
    function valid_twitter_username($input)
    {
        return preg_match('/^(\@)?[A-Za-z0-9_]+$/'$input);
    }
}

// ------------------------------------------------------------------------
if (!function_exists('valid_facebook_url')) {
    function valid_facebook_url($input)
    {
        return preg_match('/^(http\:\/\/|https\:\/\/)?(?:www\.)?facebook\.com\/(?:(?:\w\.)*#!\/)?(?:pages\/)?(?:[\w\-\.]*\/)*([\w\-\.]*)/'$input);
    }
}

// ------------------------------------------------------------------------
if (!function_exists('field_class')) {
    function field_class($input)
    {
        //Get Errors
        $errors $this->template->input_errors;

        //If error
        if (isset($errors[$input])) {
            return 'has-error';
        } elseif ($this->request->getPost()) {
            return 'has-success';
        } else {
            return '';
        }
    }




RE: my controller fails to find helper function - paulbalandan - 05-07-2025

Ok got it. In your use statements, just add this line.

use function App\Helpers\is_valid_level;


RE: my controller fails to find helper function - InsiteFX - 05-07-2025

Helpers can also be added to the app/Config/Autoload.php


RE: my controller fails to find helper function - PaulC - 05-08-2025

(8 hours ago)paulbalandan Wrote: Ok got it. In your use statements, just add this line.

use function App\Helpers\is_valid_level;

Thx for this.
Slightly dismayed at the implication though!
Do I have to list all the functions in our helpers with individual use statements?
This seems to defeat the autoloading intention, and I don't think this requirement is highlighted in the docs I have read to date?
PS I tried listing the helpers in the Autoload.php file and that didn't make any difference.
Regards, Paul

I just tried adding the use statement and still get Call to undefined function App\Controllers\is_valid_level()