thinkphp5.0ÔõÑùÉèÖùýʧҳ
I. ThinkPHP 5.0 ¹ýʧҳµÄ×÷ÓÃ
¹ýʧҳÃæÖ÷ÒªÓÃÓÚ²¶»ñÓ¦ÓóÌÐòÔÚÔËÐÐʱ·ºÆðµÄ¹ýʧ²¢Ìṩ»á¼û¹ýʧÈÕÖ¾µÄÒªÁì¡£
¹ýʧҳÃ滹֧³Öʵʱ¼Í¼¹ýʧÐÅÏ¢£¬¿ÉÒÔ¿ìËÙÅŲéÏ¢Õù¾öÉú²úÇéÐÎÖеĹýʧ¡£
II. ÉèÖùýʧҳ
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÉèÖùýʧҳÃæÐèÒªÔÚÓ¦ÓóÌÐòµÄÉèÖÃÎļþÖоÙÐУ¬ÀýÈçÔÚ config.php ÎļþÖÐÔöÌíÒÔÏÂÉèÖãº
'exception_handle' => 'app\index\exception\Http',
µÇ¼ºó¸´ÖÆ
ÆäÖÐ app\index\exception\Http ÊÇÖ¸Òì³£´¦ÀíÀàËùÔÚµÄÃüÃû¿Õ¼äºÍÀàÃû¡£Òì³£´¦ÀíÀàÐèÒª¼ÌÐø think\exception\Handle À࣬²¢ÖØд render ÒªÁìʵÏÖ×Ô½ç˵Òì³£ÐÅÏ¢µÄÊä³ö¡£
III. ¹ýʧҳµÄĬÈÏÉèÖÃ
ThinkPHP 5.0 ÖеÄĬÈϹýʧҳ°üÀ¨ÒÔÏÂÄÚÈÝ£º
Òì³£ÀàÃû
Òì³£¹ýʧÂë
Òì³£¹ýʧÐÎò
Òì³£¹ýʧÎļþ¼°ÐкÅ
Òì³£×·ËÝÐÅÏ¢
ÒÔÉÏÐÅÏ¢¿É×ÊÖú¿ìËÙ¶¨Î»¹ýʧλÖò¢¾ÙÐиú×ÙÆÊÎö¡£³ý´ËÖ®Í⣬¹ýʧҳ»¹ÌṩÁ˲Ù×÷°´Å¥£¬ÒԱ㿪·¢Õß¾ÙÐÐһЩ³£ÓòÙ×÷¡£
IV. ×Ô½ç˵¹ýʧҳ
¹ýʧҳ»¹Ö§³Ö×Ô½ç˵£¬Ö»ÐèÔÚ¿ØÖÆÆ÷ÖмÌÐø think\exception\Handle À࣬²¢ÖØд render ÒªÁì¼´¿É£¬ÀýÈ磺
namespace app\index\exception; use think\exception\Handle; class Http extends Handle { public function render(\Exception $e) { if ($e instanceof HttpException) { $status = $e->getStatusCode(); } else { $status = 404; } $data = [ 'status' => $status, 'message' => $this->getMessage($e), 'exception' => $this->isDebug() ? $this->getTrace($e) : [], ]; return json($data); } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëչʾÁËÔõÑù×Ô½ç˵Òì³£ÐÅÏ¢£¬ÔÚ±¬·¢¹ýʧʱ·µ»Ø JSON ¹¤Ç©×ÖÌõĹýʧÐÅÏ¢¡£
ÒÔÉϾÍÊÇthinkphp5.0ÔõÑùÉèÖùýʧҳµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡