在model层中的Banner.php
~~~
<?php
namespace app\api\model;
use think\Exception;
use think\Model;
class Banner
{
public static function getBannerById($id)
{
try{
1/0;
}catch (Exception $ex){
throw $ex;
}
return "banner";
}
}
~~~
2.在控制器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();
try{
$banner=BannerModel::getBannerByID($id);
}catch (Exception $ex){
$err=[
'error_code'=>10001,
'msg' =>$ex->getMessage()
];
return json($err,400);
}
return $banner;
}
}
~~~
结果
![](https://box.kancloud.cn/95a2d7fc18256140ff5394148d99bf3b_1371x489.png)