You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

更新Composer部署CodeIgniter4生产环境后应用无法运行求助

CodeIgniter4部署后执行composer update出现ENVIRONMENT未定义错误排查

我在将CodeIgniter 4应用部署到生产环境时,执行composer update或composer update --no-dev命令更新依赖。但之后运行应用或相关命令时,出现如下错误:

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

以下是我的.env文件和composer.json文件:

这是我的.env文件

#--------------------------------------------------------------------
# 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

这是我的composer.json文件

{
    "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"
    }
}

排查方案

  • 确认.env文件有效性:检查文件是否位于项目根目录,命名为.env(无后缀),且服务器或本地环境能读取该文件。部分生产环境会隐藏点开头的文件,需确保文件权限允许应用读取。
  • 手动定义ENVIRONMENT常量:在app/Config/Constants.php中添加以下代码,强制定义常量:
    define('ENVIRONMENT', env('CI_ENVIRONMENT', 'production'));
    
  • 修复文件权限:执行composer更新后,可能导致核心文件权限异常,确保system/CodeIgniter.php等文件有可读权限,项目目录权限设置正确(如web服务器用户拥有读取权限)。
  • 重置composer自动加载:执行以下命令清除缓存并重新生成自动加载文件:
    composer clear-cache
    composer dump-autoload -o
    
  • 调整框架版本:^4.0范围可能包含兼容性问题,尝试锁定到稳定版本(如^4.3),修改composer.json后重新执行composer update。

内容的提问来源于stack exchange,提问作者Akshayffb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 00:35:53