一般的に、次のことができます。
可能な限り例外/例外処理を使用する
PHPエラーを例外に変換するカスタムエラーハンドラーを登録します。たとえば、これをconfig/config.phpの先頭に配置します。
function my_error_handler($errno, $errstr, $errfile, $errline)
{
if (!(error_reporting() & $errno))
{
// This error code is not included in error_reporting
return;
}
log_message('error', "$errstr @$errfile::$errline($errno)" );
throw new ErrorException( $errstr, $errno, 0, $errfile, $errline );
}
set_error_handler("my_error_handler");
キャッチされなかった例外ハンドラーを登録し、config/config.phpにこのようなものを入れます
function my_exception_handler($exception)
{
echo '<pre>';
print_r($exception);
echo '</pre>';
header( "HTTP/1.0 500 Internal Server Error" );
}
set_exception_handler("my_exception_handler");
編集
終了ハンドラーを設定します。
function my_fatal_handler()
{
$errfile = "unknown file";
$errstr = "Fatal error";
$errno = E_CORE_ERROR;
$errline = 0;
$error = error_get_last();
if ( $error !== NULL )
{
echo '<pre>';
print_r($error);
echo '</pre>';
header( "HTTP/1.0 500 Internal Server Error" );
}
}
register_shutdown_function("my_fatal_handler");
アサーションを例外に変換するカスタムアサーションハンドラーを設定し、config/config.phpに次のようなものを配置します。
function my_assert_handler($file, $line, $code)
{
log_message('debug', "assertion failed @$file::$line($code)" );
throw new Exception( "assertion failed @$file::$line($code)" );
}
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_BAIL, 0);
assert_options(ASSERT_QUIET_EVAL, 0);
assert_options(ASSERT_CALLBACK, 'my_assert_handler');
次に、これがあなたの答えです、あなたのコントローラーでこのようなラッパーを使用してください
public function controller_method( )
{
try
{
// normal flow
}
catch( Exception $e )
{
log_message( 'error', $e->getMessage( ) . ' in ' . $e->getFile() . ':' . $e->getLine() );
// on error
}
}
全体を好みに合わせて調整およびカスタマイズできます。
お役に立てれば。
編集
また、CIshow_errorメソッドをインターセプトする必要があります。これをapplication/core/MY_exceptions.phpに配置します。
class MY_Exceptions extends CI_Exceptions
{
function show_error($heading, $message, $template = 'error_general', $status_code = 500)
{
log_message( 'debug', print_r( $message, TRUE ) );
throw new Exception(is_array($message) ? $message[1] : $message, $status_code );
}
}
また、application / config / database.phpでこの設定をFALSEのままにして、データベースエラーを例外に変換します。
$db['default']['db_debug'] = TRUE;
CIには、例外処理など、いくつかの(非常に)弱点がありますが、これはそれを修正するのに大いに役立ちます。