thinkphp5ÔõôÊä³öÏêϸµÄ±¨´íÐÅÏ¢
Ò»¡¢ÉèÖõ÷ÊÔģʽ
ʹÓõ÷ÊÔģʽ£¬ThinkPHP5Äܹ»ÔÚÒ³ÃæÉÏÏêϸÊäÍÉ»¯ÎóÐÅÏ¢¡£ÔÚ¿ª·¢ÇéÐÎÖУ¬ÎÒÃÇ¿ÉÒÔ½«µ÷ÊÔģʽ·¿ª£¬Àû±ãÎÒÃǾÙÐпª·¢¡£¿ÉÊÇ£¬ÔÚÉú²úÇéÐÎÖУ¬ÎÒÃÇÐèÒª¹Ø±Õµ÷ÊÔģʽ£¬ÒÔÃâй¶Ãô¸ÐÐÅÏ¢¡£
·¿ªµ÷ÊÔģʽµÄÒªÁìÈçÏ£º
·¿ªconfigĿ¼ÏµÄapp.phpÎļþ£¬²éÕÒ debug Ñ¡Ïî²¢½«ÆäÉèÖÃΪ true¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
·¿ªpublicĿ¼ÏµÄindex.phpÎļþ£¬²éÕÒAPP_DEBUGÑ¡Ï½«ÆäÉèÖÃΪ true¡£
ËäÈ»£¬ÔÚÉú²úÇéÐÎÖУ¬ÎÒÃÇÐèÒª½«ÕâЩѡÏî¸ÄΪ false£¬ÒÔÃâй¶Ãô¸ÐÐÅÏ¢¡£
¶þ¡¢Ê¹ÓÃÒì³£´¦Àí
³ýÁËÉèÖõ÷ÊÔģʽ£¬ÎÒÃÇ»¹¿ÉÒÔʹÓÃÒì³£´¦ÀíÀ´Êä³öÏêϸµÄ¹ýʧÐÅÏ¢¡£
Òì³£
ÔÚThinkPHP5ÖУ¬Òì³£ÊÇÖÁ¹ØÖ÷ÒªµÄ¿´·¨¡£ÈκÎÌáÐѹýʧµÄÐÅÏ¢¶¼ÊÇͨ¹ýÊä³öÒì³£À´ÊµÏֵġ£Òò´Ë£¬ÎÒÃÇÐèҪѧ»áÔõÑùʹÓÃÒì³£À´Êä³öÏêϸµÄ¹ýʧÐÅÏ¢¡£
ÔÚThinkPHP5ÖУ¬Òì³£·ÖΪÈýÖÖ£º
1£© \think\Exception£ºÍ¨Ë×Òì³£À࣬һÑùƽ³£ÓÃÓÚÓªÒµÂß¼ÏÞÖÆ£¬¼ÌÐøPHP×Ô´øµÄExceptionÀà¡£
2£© \think\exception\HttpException£ºHTTPÒì³£À࣬ÓÃÓÚHTTPÇëÇóÏà¹ØµÄÒì³£¡£
3£© \think\exception\ValidateException£ºÑéÖ¤Òì³£À࣬ÓÃÓÚÑéÖ¤Æ÷Ñé֤ʧ°ÜʱÅ׳ö¡£
Å׳öÒì³£
µ±³ÌÐò±¬·¢¹ýʧʱ£¬ÎÒÃÇ¿ÉÒÔÅ׳öÒì³££¬ÒÔ×èÖ¹³ÌÐòÔËÐв¢ÊäÍÉ»¯ÎóÐÅÏ¢¡£ÏÂÃæÊÇÒ»¸ö¼òÆӵĴúÂëʾÀý£º
if(!$result) { throw new \Exception('¹ýʧÐÅÏ¢'); }
µÇ¼ºó¸´ÖÆ
Õâ¸ö´úÂë»áÔÚ$resultΪfalseʱ£¬Å׳öÒ»¸öÒì³££¬²¢ÊäÍÉ»¯ÎóÐÅÏ¢¡£
²¶»ñÒì³£
ͨ¹ýtry…catchÓï¾ä¿éÀ´²¶»ñÒì³£²¢´òÓ¡¹ýʧÐÅÏ¢£¬ÊÇÔÚ´úÂëÖг£ÓõÄÒªÁì¡£ÏÂÃæÊÇÒ»¸ö¼òÆӵĴúÂëʾÀý£º
try { // ¿ÉÄÜ»áÅ׳öÒì³£µÄ´úÂë } catch (\Exception $e) { echo $e->getMessage(); }
µÇ¼ºó¸´ÖÆ
Õâ¸ö´úÂë»áÔÚtryÓï¾ä¿éÖÐÖ´ÐдúÂ룬ÈôÊÇÖ´ÐÐÀú³ÌÖзºÆðÁËÒì³££¬Ôò»á±»catchÓï¾ä¿é²¶»ñ£¬²¢ÊäÍÉ»¯ÎóÐÅÏ¢¡£
Èý¡¢Ê¹ÓÃÈÕÖ¾¼Í¼¹ýʧÐÅÏ¢
³ýÁËÒÔÉÏÁ½ÖÖÒªÁ죬ÎÒÃÇ»¹¿ÉÒÔʹÓÃÈÕÖ¾¼Í¼¹ýʧÐÅÏ¢¡£ÎÒÃDz»»áÏêϸÏÈÈÝThinkPHP5µÄÈÕ־ϵͳ£¬Ö»¼òÆÓ˵Ã÷ÔõÑùʹÓÃÈÕÖ¾À´¼Í¼¹ýʧÐÅÏ¢¡£
ÉèÖÃÈÕÖ¾
ÔÚconfigĿ¼ÏµÄapp.phpÎļþÖУ¬ÎÒÃÇ¿ÉÒÔÕÒµ½ÈÕÖ¾ÉèÖÃÏ¿ÉÒÔÉèÖÃÈÕÖ¾Çý¶¯ºÍÈÕÖ¾´æ´¢Â·¾¶µÈÏà¹ØÉèÖá£
¼Í¼ÈÕÖ¾
ÔÚ´úÂëÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃLogÀàÀ´¼Í¼ÈÕÖ¾¡£ÏÂÃæÊÇÒ»¸ö¼òÆӵĴúÂëʾÀý£º
use think\facade\Log; try { // ¿ÉÄÜ»áÅ׳öÒì³£µÄ´úÂë } catch (\Exception $e) { Log::error('¹ýʧÐÅÏ¢£º'.$e->getMessage()); }
µÇ¼ºó¸´ÖÆ
Õâ¸ö´úÂë»áÔÚtryÓï¾ä¿éÖÐÖ´ÐдúÂ룬ÈôÊÇÖ´ÐÐÀú³ÌÖзºÆðÁËÒì³££¬Ôò»á±»catchÓï¾ä¿é²¶»ñ£¬²¢Ê¹ÓÃLogÀà¼Í¼¹ýʧÐÅÏ¢¡£
ÒÔÉϾÍÊÇthinkphp5ÔõôÊä³öÏêϸµÄ±¨´íÐÅÏ¢µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡