1.定义路由:
Route::get('userapi/v1/banner_all','userapi/v1.Banner/getBannerList');
2.增加控制器
public function getBannerList($id)
{
$validate = new IDMustBePositiveInt();
$validate->goCheck();
$getBannerAll = BannerModel::find($id);
$this->success($getBannerAll);
exit;
}
3.增加验证
IDMustBePositiveInt.php
namespace app\common\validate;
/**
* 验证传递是否为正整数
* Class IDMustBePositiveInt
* @package app\common\validate
*/
class IDMustBePositiveInt extends BaseValidate
{
protected $rule = [
'id' => 'require|max:2'
];
}
4.增加验证基类
BaseValidate
<?php
/**
* Created by PhpStorm.
* User: 12155
* Date: 2019/4/9
* Time: 11:02
*/
namespace app\common\validate;
use app\exception\ParameException;
use think\facade\Request;
use think\Validate;
class BaseValidate extends Validate
{
/**
* @return bool
* @throws ParameException
*/
public function goCheck()
{
$request = Request::instance();
$params = $request->param();
$params['token'] = $request->header('token');
if (!$this->check($params)) {
// 1.返回字符串时 2.数组
$msgError = is_array($this->error) ? implode(
';', $this->error) : $this->error;
throw new ParameException($msgError);
}
return true;
}
}