Skip to content

Auto-loading Helpers not working #9601

Open
@adalyana

Description

@adalyana

PHP Version

8.1

CodeIgniter4 Version

4.6.1

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Linux

Which server did you use?

fpm-fcgi

Database

No response

What happened?

I'm currently using CodeIgniter version 4.5.5.

In this version, I have a helper added to app/Config/Autoload.php. This helper contains a function that checks if the environment is set to production:

function is_production() {
    return getenv("CI_ENVIRONMENT") == "production";
}

Additionally, I have a CustomLogHandler that sends error reports to Slack. It's configured in app/Config/Logger.php. Inside this custom logger, I call the is_production() function.

However, after upgrading to CodeIgniter 4.6.1, the is_production() function is no longer recognized. I had to explicitly load the helper in the constructor of the custom log class using the helper() function:

public function __construct()
{
    helper('my_helper'); // This was not needed before
}

I'm not sure if this is a bug, but I found it odd that this change was required after the upgrade. I reviewed the documentation but couldn’t find anything related to this behavior.

Let me know if this is expected or if it should be reported as a bug.

Steps to Reproduce

Create a helper in app/Helpers/my_helper.php with the following function:

function is_production() {
    return getenv("CI_ENVIRONMENT") == "production";
}

Register the helper in app/Config/Autoload.php:

public $helpers = ['my_helper'];

Create a custom log handler (e.g., App\Log\SlackLogger) and configure it in app/Config/Logger.php.

Inside the custom log class, call the is_production() function without explicitly loading the helper:

if (is_production()) {
    // Send logs to Slack
}

Run your app using CodeIgniter version 4.5.5 and confirm that everything works as expected.

Upgrade CodeIgniter to version 4.6.1.

Run the same code again. You will now get an error stating that is_production() is not recognized.

To fix it, you need to manually call helper('my_helper') in the constructor or before using the function:

public function __construct()
{
    helper('my_helper');
}

Expected Output

The is_production() function should be available in the custom log class without needing to manually call helper('my_helper'), as long as the helper is correctly listed in app/Config/Autoload.php.

This was the behavior in version 4.5.5, and I expected it to remain the same after upgrading to 4.6.1.

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions