±¾ÎĽ²ÊoÁËYII FrameworkÒi³£´¦Ài¡£*ÖÏi¸ø´o¼Ò¹(C)´o¼Ò²Î¿¼£¬¾ßÌaÈçÏ£º
Òi³£ÎÞ´¦²»ÔÚ£¬×÷Ϊ³ÌÐoÔ±£¬»iמÍÊÇΪÁË´´ÔiÕaÐ(C)Òi³££¬È»ºoÐÞ¸´ÕaÐ(C)Òi³£¶ø´aeÔڵġ£YII¿o¼Ü*a×°ÁËPHPµÄÒi³££¬ÈÃÒi³£´¦ÀiÆðÀ´¸u¼oµ¥¡£
ʹÓà YII´¦Ài´iÎoºÍÒi³£µÄÅaÖý¨£º
Äa¿ÉÒÔÔÚÈe¿ÚÎļþÖж¨ÒaYII_ENABLE_ERROR_HANDLERºÍYII_ENABLE_EXCEPTION_HANDLERΪtrue.
*Òý¢Òi³£µÄÇe¿o**
1.´¥*¢onError»oÕßonExceptionʼþ
2.ÈËΪÅ׳oÒi³£¡£ÀýÈç
throw new ExceptionClass('´iÎoÐÅÏ¢');//Òi³£µÄ»uÀa
throw new CHttpException(404,'´ËÒ³Ãae²»´aeÔÚ');//ÃaeÏo×iÖÕÓû§µÄÀa
Òi³£µÄÏÔʾÊÓͼ
µ±Ò»¸o´iÎo±»×ª*¢¸ø×e¼þCErrorHandlerµÄʱºo£¬Ëu»aÑ¡ÔñºÏÊʵÄÊÓͼÀ´ÏÔʾ´iÎo¡£
CErrorHandler»aËÑË÷ºÏÊʵÄÊÓͼÀ´ÏÔʾ´iÎoÐÅÏ¢£¬ËÑË÷µÄ˳ÐoÈçÏ£º
1. WebRoot/themes/ThemeName/views/system: ÔÚµ±Ç°Ö÷ÌaÊÓͼϵÄsystemĿ¼ÖС£
2. WebRoot/protected/views/system: ÔÚÓ¦ÓõÄĬÈÏÊÓͼµÄsystemĿ¼ÖС£
3. yii/framework/views: ÔÚYiiÌa¹(C)µÄ±e×¼ÊÓͼĿ¼ÖС£
ʹÓÃactionÀ´´¦Ài²¶×½µÄÒi³£´iÎo.
ÐÞ¸ÄÅaÖÃÎļþ£º
/yii_dev/testwebap/protected/config/main.php
'errorHandler'=>array(
// use 'site/error' action to display errors
'errorAction'=>'site/error',
),
ÓÃÀ´Ö¸¶¨´¦Ài´iÎoµÄaction¡£ÀýÈçsite controllerÖеÄerror action
actionErrorĬÈÏ´uÂeÈçÏÂ
/**
* This is the action to handle external exceptions.
*/
public function actionError()
{
if($error=Yii::app()->errorHandler->error)
{
if(Yii::app()->request->isAjaxRequest)
echo $error['message'];
else
$this->render('error', $error);
}
}
ÔÚÕa¸o¶¯×÷ÖУ¬Ê×ÏÈ´ÓCErrorHandler::errorÖÐÈ¡µÃÏeϸµÄ´iÎoÐÅÏ¢¡£Èç¹uÈ¡µÃµÄÐÅϢǿգ¬¾ÍʹÓÃCErrorHandler::errorµ»ØµÄÐÅÏ¢À´aÖȾerrorÊÓͼ¡£CErrorHandler::error*µ»ØµÄÐÅÏ¢ÊÇÒ»¸oÊý×e£¬½a¹¹ÈçÏ£º
code: HTTP ״̬Âe£¨±ÈÈç 403, 500£(C)£»
type: ´iÎoÀaÐÍ£¨±ÈÈç CHttpException, PHP Error£(C)£»
message: ´iÎoÐÅÏ¢£»
file: ¢Éu´iÎoµÄPHPÎļþÃu£»
line: ´iÎoËuÔÚµÄÐУ»
trace: ´iÎoµÄµ÷ÓÃÕ»ÐÅÏ¢£»
source: ¢Éu´iÎoµÄ´uÂeµÄÉÏÏÂÎÄ¡£
Òi³£ÈÕÖ¾
Ò»¸oerror¼¶±ðµÄ´iÎoÐÅÏ¢»aÔÚ´iÎo¢Éuʱºo±»¼Ç¼¡£Èç¹uÕa¸o´iÎoÊÇÓÉPHP warning »o noticeÒý¢µÄ£¬ÄÇôÕa¸oÏuÏ¢½«»a±»¼Ç¼ÔÚphpÕa¸oÖÀaÖУ»Èç¹u´iÎoÐÅÏ¢ÊÇÓÉ䲶»ñµÄÒi³£ËuÒýÆðµÄ£¬ÄÇôÖÀa½«ÊÇexception.ExceptionClassName£¨¶ÔÓÚCHttpExceptionÀ´Ëµ£¬ËuµÄstatusCodeÒ²½«±»×¼Óµ½ÖÀaÃuÖУ(C)¡£¿ª¢Õß¿ÉÒÔʹÓÃÕaÐ(C)¼Ç¼À´¼a²aÓ¦ÓÃÖ´ÐÐʱºoµÄ´iÎoÐÅÏ¢Òi³£´¦ÀiµÄ½*¨¡£
¸u¶a¹ØÓÚYiiÏa¹ØÄÚÈݸÐÐËȤµÄ¶ÁÕ߿ɲe¿´±¾Õ¾×¨Ìa£º¡¶Yii¿o¼ÜÈeÃż°³£Óü¼ÇÉ×ܽa¡¡¢¡¶[phpÓÅÐa¿ª¢¿o¼Ü×ܽa](http://www.jb51.net/Special/155.htm)¡¡¢¡¶smartyÄ£°aÈeÃÅ»u´¡½Ì³Ì¡¡¢¡¶phpÈÕÆÚÓeʱ¼aÓÃ*¨×ܽa¡¡¢¡¶phpÃaeÏo¶ÔÏo³ÌÐoÉe¼ÆÈeÃŽ̡̳¡¢¡¶php×Öu´®(string)Óè×ܽa¡¡¢¡¶php+mysqlÊý¾Ý¿a²Ù×÷ÈeÃŽ̡̳¼°¡¶php³£¼uÊý¾Ý¿a²Ù×÷¼¼ÇÉ»a×Ü¡*
Ï£Íu±¾ÎÄËuÊo¶Ô´o¼Ò»uÓÚYii¿o¼ÜµÄPHP³ÌÐoÉe¼ÆÓÐËu°iÖu¡£