CodeIgniter4: Auto-loading Helpers not working
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
About this issue
- Original URL
- State: closed
- Created a year ago
- Comments: 17 (8 by maintainers)
Although we expect the upgrade instructions to be followed in full, I see we could have done more to highlight the importance of this change. Mentioning Kint explicitly in the section above should help eliminate any confusion.
Thanks for all the comments and help. I found the root of the issue. Let me explain:
We allways update our project using:
composer updateTo understand this issue I started testing version by version, starting from 4.5.6 (we were previously on 4.5.5). I discovered that the issue appears when upgrading from 4.5.8 to 4.6.0 — that’s where I could reproduce the problem I reported.
Once I had the version that caused the error, I ran:
php spark config:checkAnd got the following error:
The important part here is:
Error: Undefined constant Kint\Renderer\AbstractRenderer::SORT_FULLIn version 4.5.8, this constant is defined in app/Config/Kint.php:
https://github.com/codeigniter4/CodeIgniter4/blob/v4.5.8/app/Config/Kint.php#L44
But looking at the changes made from 4.5.8 to 4.6.0: https://github.com/codeigniter4/CodeIgniter4/compare/v4.5.8...v4.6.0#diff-907319ab25c4e05e576a1754147d0b980076b79b4b48f92d99314e7366c0f816L550
That line was removed from app/Config/Kint.php.
So I assume that since the autoloader fails at that point, it doesn’t load my helper either — which causes the
is_production()function to be undefined.The error wasn’t visible at first because I tested the upgrade with a request to one of the APIs. But once I ran a console command, the error showed up clearly.
Now, looking at the upgrade documentation:
https://github.com/codeigniter4/CodeIgniter4/blob/5c7181e45bf69198b225681b7178d4135ee2fdaf/user_guide_src/source/installation/upgrade_460.rst
https://codeigniter4.github.io/userguide/installation/upgrade_460.html
There’s no mention that this change affects the application behavior.
✅ How I fixed it: In app/Config/Kint.php, I simply commented out this line:
public int $richSort = AbstractRenderer::SORT_FULL;After that, everything works fine again.
❓ My question is: Is this a bug in the upgrade process? Or was it just missing from the documentation that this change must be made manually after running composer update?
This is enable file locator cache?