本文实例讲述了thinkphp5桂林风景框架异常处理操作。分矩形的对角线享给大家供大家参考,具体如下:
有时候服务端会报出我们无法感知的错误,tp5默认会自动渲染错误的形式,生产环境中这样的形式并不是我们想要的。
1.exception\handle.php下的render方法需要覆盖
创建apihandleexception.php
<?phpnamesp功用的近义词ace app\common\lib\exception;u think\exception\handle;class apihandleexception extends handle { /** * http 状态码 * @var int */ public $httpcode = 500; public function render(\exception $e) { return show(0, $e->失败是什么getmessage(), [], $this->httpcode); }}
2.修改config.php的exception_handle配置项
我们在判断一个数据是否合法的时候,若不合法则抛出异常。
例如:
if($data['msg'] != 1){ throw exception('数据异常'); }
使用内置的异常http状态码始终为500
1.创建apiexception.php
<?phpnamespace app\common\lib\exception;u think\exception;class apiexception extends exception { public $message = ''; public $httpcode = 500; public $code = 0; /** * @param string $message * @param int $httpcode * @param int $code */ public function __construct($message = '', $httpcode = 0, $code = 0) { $this->httpcode = $httpcode; $this->message = $message; $this->code = $code; }}
2.对apihandleexception.php改写
<?phpnamespace app\common\lib\exception;u think\exception\handle;class apihandleexception extends handle { /** * http 状态码 * @var int */ public $httpcode = 500; public function render(\exception $e) { if ($e instanceof apiexception) { $this->httpcode = $e->httpcode; } return show(0, $e->getmessage(), [], $this->httpcode); }}
在开发环境的时候依旧使用异常渲染的模式
在apihandleexc魔兽世界暗月岛怎么去eption.php中添加代码
if(config('app_debug') == true) { return parent::render($e); }
本文发布于:2023-04-08 17:49:39,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/600794ae87ae1f4539dba71578142645.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:Thinkphp5框架异常处理操作实例分析.doc
本文 PDF 下载地址:Thinkphp5框架异常处理操作实例分析.pdf
留言与评论(共有 0 条评论) |