model 返回一个null
<?php
namespace app\api\model;
use think\Exception;
class Banner
{
public static function getBannerByID($id){
return null;
}
}
controller 抛出异常
<?php
namespace app\api\controller\v1;
use app\api\model\Banner as BannerModel;
use app\api\validate\IDMustBePositiveInt;
use app\lib\exception\BannerMissException;
use think\Exception;
class Banner
{
/**
* 获取指定id的banner信息
* @url/banner/:id
* @http GET
* @id banner的id号
*
*/
public function getBanner($id){
$validate = new IDMustBePositiveInt();
$validate->goCheck();
$banner = BannerModel::getBannerByID($id);
if(!$banner){
throw new BannerMissException();//抛出异常
//必须继承Exception
}
return $banner;
}
}
exception 进行处理
BannerMiss