多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
1.更改控制器Banner.php ~~~ <?php namespace app\api\controller\v1; use app\api\validate\IDMustBePostiveInt; use app\api\model\Banner as BannerModel; use think\Exception; class Banner{ //获取指定id的banner信息 public function getBanner($id) { (new IDMustBePostiveInt())->goCheck(); $banner=BannerModel::getBannerByID($id); return $banner; } } ~~~ 2.在config配置`exception_handle` ~~~ // 异常处理handle类 留空使用 \think\exception\Handle //'exception_handle' => '', 'exception_handle' => 'app\lib\exception\ExceptionHandler', ~~~ 3.在`app\lib\exception`下新建ExceptionHandler.php文件 ~~~ <?php namespace app\lib\exception; use Exception; use think\exception\Handle; class ExceptionHandler extends Handle { public function render(Exception $e) { return json('hello 异常'); } } ~~~ 4.在`app\lib\exception`下新建BaseException.php文件 ~~~ <?php namespace app\lib\exception; use think\Exception; /** * Class BaseException * 自定义异常类的基类 */ class BaseException extends Exception { public $code = 400; //public $msg = 'invalid parameters'; public $msg = '参数错误'; public $errorCode = 999; } ~~~ 5.在`app\lib\exception`下新建BannerMissException.php文件 ~~~ <?php namespace app\lib\exception; class BannerMissException extends BaseException { public $code = 404; public $msg = '请求的Banner不存在'; public $errorCode = 40000; } ~~~ 结果 ![](https://box.kancloud.cn/861f719c67faae6f6e6139ce93bff8db_852x467.png)