Codeigniter 4: Resolved Errors after upgrade from codeigniter 4.3.x to 4.4.0

30th Dec 2023 | category: Frameworks | Hits: 606 Codeigniter 4: Resolved Errors after upgrade from codeigniter 4.3.x to 4.4.0

Full Error Message
Fatal error: Uncaught Error: Undefined constant "CodeIgniter\ENVIRONMENT" in C:\xampp\htdocs\agripoint\vendor\codeigniter4\framework\system\CodeIgniter.php:595 Stack trace: #0 C:\xampp\htdocs\agripoint\vendor\codeigniter4\framework\system\CodeIgniter.php(207): CodeIgniter\CodeIgniter->bootstrapEnvironment() #1 C:\xampp\htdocs\agripoint\index.php(55): CodeIgniter\CodeIgniter->initialize() #2 {main} thrown in C:\xampp\htdocs\agripoint\vendor\codeigniter4\framework\system\CodeIgniter.php on line 595

Whenever your upgrading your system to the latest version of any updates or patches, always do the following:-

  • BE Calm
  • BACKUP your system/website/App files & its Database.
  • Always follow the manual specifically the area showing what has been changed from the current version of core files and how it will after your system/App

Now for those moving from Codeigniter 4.3.x to 4.4.0 and faced the above error, lets get started.

Step 1: - Launch command line

Using composer on command line in your project root - run
composer update

Am using "Visual Studio Code" thus the screenshot but whichever command line you prefer either window's or Mac's or Linux's will work so long as composer is installed.

Screenshot showing upgrade through composer Screenshot showing upgrade through composer

NOTE: No need to repeat this step, if already done - just move to the next steps.

Step 2 - Test & Follow Manual Guidelines

Testing I mean check on your system to see errors generated.

Screenshot - still showing errors on App Screenshot - still showing errors on App

From Codeigniter's guideline - https://codeigniter4.github.io/CodeIgniter4/installation/upgrade_440.html#app-config-routing-php.
In summary grab the following files from framework directory (.......vendor/codeigniter4/framework/app/Config):-

  • Routing.php
  • Toolbar.php
  • Events.php
  • Cookie.php
  • Security.php
  • Session.php

And paste them in your (app/Config)
Then in our app/Config/Routes.php file remove the following because they are now in app/Config/Routing.php.

Screenshot: REMOVE FROM Routes.php file Screenshot: REMOVE FROM Routes.php file
Screenshot: REMOVE FROM Routes.php file Screenshot: REMOVE FROM Routes.php file

Add the line in your Routes.php at the top.

use CodeIgniter\Router\RouteCollection;

STEP 3 - Workon your App.php file.

So much has been moved out of the App.php file and my recommendation is to backup your App.php and pick a new from framework directory.
- Rename App.php to say OldApp.php
- Copy a new file from (vendor/codeigniter4/framework/app/Config) into your app/Config.


Ensure the following lines have same info as your old App.php file.

  • public string $baseURL = '';
  • public string $indexPage = '';
  • public string $uriProtocol = '';
  • public string $appTimezone = '';

NOTE: Ensure your crucial settings in App.php are updated in the new one.
Also note that all settings of cookies, session have been moved out of the App.php file.

STEP 4 - Work on your index.php

At this point, you realize the errors are still showing. The last step is to configure the index.php file.

The index.php file can directly be in root directory or in the default public/index.php. Mine is in root directory.

- Best to open the index.php file in (vendor/codeigniter4/framework/public), From it add the following into your index.php

Screenshot: Remove this code from index.php file Screenshot: Remove this code from index.php file
Screenshot: Add this code index.php  file between line 14 - 22 Screenshot: Add this code index.php file between line 14 - 22

Below line 44 "(new CodeIgniter\Config\DotEnv(ROOTPATH))->load();"
Add the below line.

********************************************************
// Define ENVIRONMENT
if (! defined('ENVIRONMENT')) {
define('ENVIRONMENT', env('CI_ENVIRONMENT', 'production'));
}
************************************************

after "$app->run();" add the following runs.
Add the below line.

************************************************************
// Save Config Cache
// $factoriesCache->save('config');
// ^^^ Uncomment this line if you want to use Config Caching.

// Exits the application, setting the exit code for CLI-based applications
// that might be watching.
exit(EXIT_SUCCESS);
************************************************************

Your codegniter 4 App should be now working fine with version4.4.0 and all errors should be gone.