我已阅读官方文档,他们只说我应该在主index.php文件的顶部拥有error_reporting()函数。 但是我在我的项目中没有index.php
文件。我的基本控制器称为core
,因此要进入主索引,我需要前往www.mysite.dom/core/
。所以我猜这个错误报告功能应该放在这个控制器里面?然后我想知道应该将其放在控制器的哪个位置以及放什么内容以禁用报告。感谢大家的帮助,我想我可能漏了什么 :/
我已阅读官方文档,他们只说我应该在主index.php文件的顶部拥有error_reporting()函数。 但是我在我的项目中没有index.php
文件。我的基本控制器称为core
,因此要进入主索引,我需要前往www.mysite.dom/core/
。所以我猜这个错误报告功能应该放在这个控制器里面?然后我想知道应该将其放在控制器的哪个位置以及放什么内容以禁用报告。感谢大家的帮助,我想我可能漏了什么 :/
这是新 Codeigniter 项目的典型结构:
- application/
- system/
- user_guide/
- index.php <- this is the file you need to change
我通常在CI的index.php文件中使用这段代码。只需要将local_server_name更改为您本地Web服务器的名称即可。
使用此代码,您可以在不每次更改index.php的情况下将站点部署到生产服务器上。
// Domain-based environment
if ($_SERVER['SERVER_NAME'] == 'local_server_name') {
define('ENVIRONMENT', 'development');
} else {
define('ENVIRONMENT', 'production');
}
/*
*---------------------------------------------------------------
* ERROR REPORTING
*---------------------------------------------------------------
*
* Different environments will require different levels of error reporting.
* By default development will show errors but testing and live will hide them.
*/
if (defined('ENVIRONMENT')) {
switch (ENVIRONMENT) {
case 'development':
error_reporting(E_ALL);
break;
case 'testing':
case 'production':
error_reporting(0);
ini_set('display_errors', 0);
break;
default:
exit('The application environment is not set correctly.');
}
}
将CI的index.php文件更改为:
if ($_SERVER['SERVER_NAME'] == 'local_server_name') {
define('ENVIRONMENT', 'development');
} else {
define('ENVIRONMENT', 'production');
}
if (defined('ENVIRONMENT')){
switch (ENVIRONMENT){
case 'development':
error_reporting(E_ALL);
break;
case 'testing':
case 'production':
error_reporting(0);
break;
default:
exit('The application environment is not set correctly.');
}
}
$db['default']['db_debug'] = FALSE;
此外,您可以使用active_group作为development和production来匹配环境。https://www.codeigniter.com/user_guide/database/configuration.html
$active_group = 'development';
$db['development']['hostname'] = 'localhost';
$db['development']['username'] = '---';
$db['development']['password'] = '---';
$db['development']['database'] = '---';
$db['development']['dbdriver'] = 'mysql';
$db['development']['dbprefix'] = '';
$db['development']['pconnect'] = TRUE;
$db['development']['db_debug'] = TRUE;
$db['development']['cache_on'] = FALSE;
$db['development']['cachedir'] = '';
$db['development']['char_set'] = 'utf8';
$db['development']['dbcollat'] = 'utf8_general_ci';
$db['development']['swap_pre'] = '';
$db['development']['autoinit'] = TRUE;
$db['development']['stricton'] = FALSE;
$db['production']['hostname'] = 'localhost';
$db['production']['username'] = '---';
$db['production']['password'] = '---';
$db['production']['database'] = '---';
$db['production']['dbdriver'] = 'mysql';
$db['production']['dbprefix'] = '';
$db['production']['pconnect'] = TRUE;
$db['production']['db_debug'] = FALSE;
$db['production']['cache_on'] = FALSE;
$db['production']['cachedir'] = '';
$db['production']['char_set'] = 'utf8';
$db['production']['dbcollat'] = 'utf8_general_ci';
$db['production']['swap_pre'] = '';
$db['production']['autoinit'] = TRUE;
$db['production']['stricton'] = FALSE;
无需输入长查询来禁用CodeIgniter中的错误报告。在页面中使用error_reporting(0);
即可禁用该页面上显示的错误。
<?php
error_reporting(0);
?>
我知道我很晚了,但你可以这样做:在index.php文件中找到下面的代码
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');
将 development 替换为 production
ini_set('display_errors', 0);
添加到index.php
中吗?我有点困惑,因为你说“使用此代码,您可以在不更改 index.php 的情况下部署您的网站”,但实际上看起来你确实在更改index.php
,我错了吗? - user1227065define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');
(后面不需要if
)。 - CPHPython