phpenvironment-variablescodeigniter-4.env

After updating Composer to deploy the CodeIgniter 4 app on production, the app won't run


When I try to deploy the app to production, I run composer update or composer update --no-dev. This updates the dependencies. After that, if I check or run any related commands or try to run the app, it gives me the error I mentioned below. I'm having trouble understanding what might be causing this issue.

PHP Fatal error:  Uncaught Error: Undefined constant "CodeIgniter\ENVIRONMENT" in C:\xampp\htdocs\SDAHSS\vendor\codeigniter4\framework\system\CodeIgniter.php:583
Stack trace:
#0 C:\xampp\htdocs\SDAHSS\vendor\codeigniter4\framework\system\CodeIgniter.php(203): CodeIgniter\CodeIgniter->bootstrapEnvironment()    
#1 C:\xampp\htdocs\SDAHSS\spark(83): CodeIgniter\CodeIgniter->initialize()
#2 {main}
  thrown in C:\xampp\htdocs\SDAHSS\vendor\codeigniter4\framework\system\CodeIgniter.php on line 583

Fatal error: Uncaught Error: Undefined constant "CodeIgniter\ENVIRONMENT" in C:\xampp\htdocs\SDAHSS\vendor\codeigniter4\framework\system\CodeIgniter.php:583
Stack trace:
#0 C:\xampp\htdocs\SDAHSS\vendor\codeigniter4\framework\system\CodeIgniter.php(203): CodeIgniter\CodeIgniter->bootstrapEnvironment()    
#1 C:\xampp\htdocs\SDAHSS\spark(83): CodeIgniter\CodeIgniter->initialize()
#2 {main}
  thrown in C:\xampp\htdocs\SDAHSS\vendor\codeigniter4\framework\system\CodeIgniter.php on line 583
PS C:\xampp\htdocs\SDAHSS> 

I have tried running almost all possible commands but am still getting the above error. I am not understanding what is wrong with the application. Please help, Thank you in advance.

This is my .env file

#--------------------------------------------------------------------
# Example Environment Configuration file
#
# This file can be used as a starting point for your own
# custom .env files, and contains most of the possible settings
# available in a default install.
#
# By default, all of the settings are commented out. If you want
# to override the setting, you must un-comment it by removing the '#'
# at the beginning of the line.
#--------------------------------------------------------------------

#--------------------------------------------------------------------
# ENVIRONMENT
#--------------------------------------------------------------------

CI_ENVIRONMENT = development

#--------------------------------------------------------------------
# APP
#--------------------------------------------------------------------

# app.baseURL = ''
# If you have trouble with `.`, you could also use `_`.
# app_baseURL = ''
# app.forceGlobalSecureRequests = false
# app.CSPEnabled = false

#--------------------------------------------------------------------
# DATABASE
#--------------------------------------------------------------------

database.default.hostname = localhost
database.default.database = sdas
database.default.username = root
database.default.password = 
database.default.DBDriver = MySQLi
database.default.DBPrefix =
database.default.port = 3306

# database.tests.hostname = localhost
# database.tests.database = ci4_test
# database.tests.username = root
# database.tests.password = root
# database.tests.DBDriver = MySQLi
# database.tests.DBPrefix =
# database.tests.port = 3306

#--------------------------------------------------------------------
# CONTENT SECURITY POLICY
#--------------------------------------------------------------------

# contentsecuritypolicy.reportOnly = false
# contentsecuritypolicy.defaultSrc = 'none'
# contentsecuritypolicy.scriptSrc = 'self'
# contentsecuritypolicy.styleSrc = 'self'
# contentsecuritypolicy.imageSrc = 'self'
# contentsecuritypolicy.baseURI = null
# contentsecuritypolicy.childSrc = null
# contentsecuritypolicy.connectSrc = 'self'
# contentsecuritypolicy.fontSrc = null
# contentsecuritypolicy.formAction = null
# contentsecuritypolicy.frameAncestors = null
# contentsecuritypolicy.frameSrc = null
# contentsecuritypolicy.mediaSrc = null
# contentsecuritypolicy.objectSrc = null
# contentsecuritypolicy.pluginTypes = null
# contentsecuritypolicy.reportURI = null
# contentsecuritypolicy.sandbox = false
# contentsecuritypolicy.upgradeInsecureRequests = false
# contentsecuritypolicy.styleNonceTag = '{csp-style-nonce}'
# contentsecuritypolicy.scriptNonceTag = '{csp-script-nonce}'
# contentsecuritypolicy.autoNonce = true

#--------------------------------------------------------------------
# COOKIE
#--------------------------------------------------------------------

# cookie.prefix = ''
# cookie.expires = 0
# cookie.path = '/'
# cookie.domain = ''
# cookie.secure = false
# cookie.httponly = false
# cookie.samesite = 'Lax'
# cookie.raw = false

#--------------------------------------------------------------------
# ENCRYPTION
#--------------------------------------------------------------------

# encryption.key =
# encryption.driver = OpenSSL
# encryption.blockSize = 16
# encryption.digest = SHA512

#--------------------------------------------------------------------
# HONEYPOT
#--------------------------------------------------------------------

# honeypot.hidden = 'true'
# honeypot.label = 'Fill This Field'
# honeypot.name = 'honeypot'
# honeypot.template = '<label>{label}</label><input type="text" name="{name}" value=""/>'
# honeypot.container = '<div style="display:none">{template}</div>'

#--------------------------------------------------------------------
# SECURITY
#--------------------------------------------------------------------

# security.csrfProtection = 'cookie'
# security.tokenRandomize = false
# security.tokenName = 'csrf_token_name'
# security.headerName = 'X-CSRF-TOKEN'
# security.cookieName = 'csrf_cookie_name'
# security.expires = 7200
# security.regenerate = true
# security.redirect = false
# security.samesite = 'Lax'

#--------------------------------------------------------------------
# SESSION
#--------------------------------------------------------------------

# session.driver = 'CodeIgniter\Session\Handlers\FileHandler'
# session.cookieName = 'ci_session'
# session.expiration = 7200
# session.savePath = null
# session.matchIP = false
# session.timeToUpdate = 300
# session.regenerateDestroy = false

#--------------------------------------------------------------------
# LOGGER
#--------------------------------------------------------------------

# logger.threshold = 4

#--------------------------------------------------------------------
# CURLRequest
#--------------------------------------------------------------------

# curlrequest.shareOptions = true

This is my composer.json file

{
    "name": "codeigniter4/zvadevelopers",
    "type": "project",
    "description": "CodeIgniter4 starter app",
    "homepage": "https://codeigniter.com",
    "license": "MIT",
    "require": {
        "php": "^7.4 || ^8.0",
        "codeigniter4/framework": "^4.0",
        "intervention/image": "^2.7"
    },
    "require-dev": {
        "fakerphp/faker": "^1.9",
        "mikey179/vfsstream": "^1.6",
        "phpunit/phpunit": "^9.1"
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "autoload": {
        "exclude-from-classmap": [
            "**/Database/Migrations/**"
        ]
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\Support\\": "tests/_support"
        }
    },
    "scripts": {
        "test": "phpunit"
    },
    "support": {
        "forum": "https://forum.codeigniter.com/",
        "source": "https://github.com/codeigniter4/CodeIgniter4",
        "slack": "https://codeigniterchat.slack.com"
    }
}


Solution

  • I don't know CodeIgniter, but I did some digging. Looking at this line you can see that, in version 4.4.0 something changed - ENVIRONMENT variable should be set in spark or index.php. I've created a new sample app using version 4.4.1 and in fact the ENVIRONMENT variable is set in those files:

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

    Maybe your spark and index.php files was created using pre 4.4.0 version, and now you are using post 4.4.0 version but you haven't updated the spark and index.php files. Please try to create a new sample CodeIgniter app somewhere using composer create-project codeigniter4/appstarter sample-app and copy those files from there to your app.