<?php
namespace app\api\controller\v1;
use app\api\validate\IDMustBePostiveInt;
use app\api\validate\TestValidate;
use think\Validate;
class Banner
{
/**
* 获取指定id的banner信息
* @url /banner/:id
* @http GEt
* @id banner的id号
*i
*/
public function getBanner($id)
{
(new IDMustBePostiveInt())->goCheck();//拦截器
$c = 1;
//$validate = new IDMustBePostiveInt();
//$validate->goCheck();
// $banner = BannerModel::getBannerById($id);
// if(!$banner){
// throw new MissException([
// 'msg' => '请求banner不存在',
// 'errorCode' => 40000
// ]);
// }
// return $banner;
}
}
<?php
namespace app\api\validate;
use think\Exception;
use think\Request;
use think\Validate;
class BaseValidate extends Validate
{
public function goCheck()
{
$request = Request::instance();
$params = $request->param();
$result = $this->check($params);
if (!$result){
$error = $this->error;
throw new Exception($error);
}
else{
return true;
}
}
}
<?php
namespace app\api\validate;
use think\Validate;
class IDMustBePostiveInt extends Validate
{
protected $rule = [
'id' => 'require|isPositiveInteger'
];
protected function isPositiveInteger($value, $rule = '', $data = '', $field = '')
{
if (is_numeric($value) && is_int($value + 0) && ($value + 0)>0) {
return true;
}
else {
return $field .'必须是正整数';
}
}
}