ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
参数请求验证自定义 写一个关于基础类的验证方法 BaseValidate.php ``` <?php namespace app\api\validate; use think\Exception; use think\Request; use think\Validate; class BaseValidate extends Validate { public function goCheck(){ //获取http传入的参数 //对这些参数做检验 $request = Request::instance(); $params = $request->param(); $result = $this->check($params); if(!$result){ $error = $this->error; throw new Exception($error); }else{ // echo '111'; return true; } } } ``` 对一个传入$id 进行验证 IdValidate.php ``` <?php namespace app\api\validate; use \app\api\validate\BaseValidate; class IdValidate extends BaseValidate { protected $rule = [ 'id' =>"require|int" ]; } ``` 在控制器中进行使用 ``` (new IdValidate())->goCheck(); ``` 异常错误自定义 创建一个异常目录 exception 创建第一个基础类异常文件BaseException.php 此文件定义HTTP状态码 错误信息 以及自定义错误码 ``` <?php namespace app\lib\exception; use think\Exception; class BaseException extends Exception { //HTTP 状态码 404 200 public $code = 400; //错误信息 public $msg = '参数错误'; //自定义的错误码 public $errorcode = 10000; } ``` 创建文章请求异常文件 ArticleMissException.php ``` <?php namespace app\lib\exception; class ArticleMissException extends BaseException { public $code = 404; public $msg = '请求的文章不存在'; public $errorcode = 40000; } ``` 修改(TP5的配置文件config.php搜:exception_handle): // 异常处理handle类 留空使用 \think\exception\Handle ``` // 异常处理handle类 留空使用 \think\exception\Handle 'exception_handle' => 'app\lib\exception\ExceptionHandler', ``` 异常处理文件 ExceptionHandler .php ``` <?php namespace app\lib\exception; use Exception; use think\exception\Handle; use app\lib\exception\BaseException; use think\Request; class ExceptionHandler extends Handle { private $code; private $msg; private $errorcode; //需要返回当前客户的URL路径 public function render(Exception $e){ if($e instanceof BaseException){ //如果是自定义异常 $this->code = $e->code; $this->msg = $e->msg; $this->errorcode = $e->errorcode; }else{ $this->code = 500; $this->msg = '服务器内部错误'; $this->errorcode = 999; $this->recordErrorLog($e); //记录到日记中 } $request = Request::instance(); $result = [ 'msg'=>$this->msg, 'error_code'=>$this->errorcode, 'request_url'=>$request->url() ]; return json($result,$this->code); } /** * 未知错误写入日记 * @param Exception $e * 1、初始化TP5日记 * 2、Log::record() 记录日志信息到内存 * $e->getMessage() 日记信息 * 'error' 日记级别 */ private function recordErrorLog(Exception $e){ Log::init([ 'type'=>'File', 'path'=>APP_PATH.'lib/exception/log/', 'level'=>['error'] ]); Log::record($e->getMessage(),'error'); } } ``` 上面ExceptionHandler .php 第一个 use Exception 原因: handle下的render方法里(Exception $e)用的是use Exception,不能使用 use think\Exception; 将use think\exception 改为 use Exception 验证异常: 模型页面 Article.php ``` <?php namespace app\api\model; use think\Model; class Article extends Model { public static function getArticleByID($id){ // TODO:根据文章 ID 获取文章 信息 return null; } } ``` 控制器页面 Article.php ``` <?php namespace app\api\controller\v1; use app\api\validate\IdValidate; use app\api\model\Article as ArticleModel; use app\lib\exception\ArticleMissException; class Article { /*** * 获取指定id的文章信息 * @url /Article/:id * @http GET * @id Article的id */ public function getArticle($id){ (new IdValidate())->goCheck(); $artcile= ArticleModel::getArticleByID($id); if(!$artcile){ //throw new Exception('内部错误'); //测试日记内部错误 throw new ArticleMissException(); } return $artcile; } } ```