首页 > 其他分享 >webman:用think-validate做验证器(v1.5.7)

webman:用think-validate做验证器(v1.5.7)

时间:2023-08-28 15:35:49浏览次数:48  
标签:use webman app id v1.5 validate think

一,官方文档地址:

https://www.workerman.net/doc/webman/components/validation.html

二,安装组件:

1,安装

liuhongdi@lhdpc:/data/webman/imageadmin$ composer require topthink/think-validate

2,安装后查看版本:

liuhongdi@lhdpc:/data/webman/imageadmin$ composer show topthink/think-validate
name     : topthink/think-validate
descrip. : think validate
keywords :
versions : * v2.0.2
...

三,代码:

1,app/validate/ImageList.php

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 <?php   namespace app\validate;   use think\Validate;   class ImageList extends Validate {     /**      * 定义验证规则      * 格式:'字段名' =>  ['规则1','规则2'...]      *      * @var array      */     protected $rule = [         'categoryId'  => 'require|number|gt:0',         'channelId'  => 'require|length:4|alphaNum',     ];       /**      * 定义错误信息      * 格式:'字段名.规则名' =>  '错误信息'      *      * @var array      */     protected $message = [         'categoryId.require' => '分类id必须',         'categoryId.number'     => '分类id需要是整数',         'categoryId.gt'     => '分类id需大于0',         'channelId.require' => '频道id必须',         'channelId.length'     => '频道id长度不为4',         'channelId.alphaNum'     => '频道id需由字母和数字构成',     ]; }

2,调用

    app/controller/ImageController.php

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 <?php namespace app\controller;   use support\Request; use app\result\Result; use support\Log; use think\facade\Db; use think\facade\Cache;     use app\model\Comment as CommentModel;   class ImageController {     //图片的列表     public function list(Request $request)     {         $validate = new \app\validate\ImageList;             $res = $validate->check($request->get());             //检查是否合法             if ($res == false) {                 $error = $validate->getError();                 return Result::ErrorCode(422,$error);             }          //获取数据          $path = '_image_imageborder';          $comObj = new CommentModel();          $rows = $comObj->list($path);          return  Result::Success($rows);     } }

说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/08/23/webman-yong-thinkvalidate-zuo-yan-zheng-qi-v1-5-7/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: [email protected]

四,测试效果:

五,查看webman版本:

liuhongdi@lhdpc:/data/webman/imageadmin$ composer show workerman/webman-framework
name     : workerman/webman-framework
descrip. : High performance HTTP Service Framework.
keywords : High Performance, http service
versions : * v1.5.7
...
 阅读: 22

标签:use,webman,app,id,v1.5,validate,think
From: https://www.cnblogs.com/architectforest/p/17662379.html

相关文章

  • webman:自定义配置文件(v1.5.7)
    一,官方文档地址:https://www.workerman.net/doc/webman/config.html二,代码:1,.env:GOODS_IMAGE_DIR=/var/www/html/goodsImageGOODS_IMAGE_HOST=http://192.168.219.62,config/images.php12345<?phpreturn[   "goodsImageDir"=>getenv(......
  • Struts2输入校验以及错误信息处理(1)——用Action中定义的validate()方法进行校验
    Struts2的输入校验有两种方式:一种是用Action中定义的validate()方法进行校验,一种是用Struts2定义好的校验框架进行校验。前者里面的逻辑判断要自己写,而后者只需要传递相应的参数即可。不管是哪种方式,程序执行的流程都是一样的,执行流程如下:1、对表单传递过来的数据,先进行类型转换2、......
  • webman:配置端口/日志等(v1.5.7)
     一,文档地址:https://www.workerman.net/doc/webman/others/security.htmlhttps://www.workerman.net/doc/webman/request.htmlhttps://www.workerman.net/doc/webman/config.html说明:刘宏缔的架构森林—专注it技术的博客,网站:https://blog.imgtouch.com原文: https://b......
  • webman:全局中间件:记录访问日志(v1.5.7)
    一,官方文档地址:https://www.workerman.net/doc/webman/middleware.html二,php代码1,配置中间件:config/middleware.php12345678910111213141516171819<?php/** *Thisfileispartofwebman. * *LicensedunderTheMITLicense......
  • webman:用thinkorm访问数据库(v1.5.7)
    一,官方文档地址:https://www.workerman.net/doc/webman/db/thinkorm.html二,安装组件liuhongdi@lhdpc:/data/webman/imageadmin$composerrequire-Wwebman/think-orm./composer.jsonhasbeenupdatedRunningcomposerupdatewebman/think-orm--with-all-dependencies......
  • webman:用thinkcache访问redis(v1.5.7)
    一,官方文档地址:https://www.workerman.net/doc/webman/db/thinkcache.html二,安装组件liuhongdi@lhdpc:/data/webman/imageadmin$composerrequire-Wwebman/think-cache三,配置redisconfig/thinkcache.php,按自己的实际情况配置12345678910111213......
  • webman:配置异常处理返回json格式(v1.5.7)
    一,添加一个除0错的异常代码:页面显示效果如图:二,配置:php代码1,config/123456789101112131415161718<?php/** *Thisfileispartofwebman. * *LicensedunderTheMITLicense *Forfullcopyrightandlicenseinformation......
  • webman:安装/创建项目(v1.5.7)
    一,官方文档:1,官方站:https://www.workerman.net/webman2,安装文档:https://www.workerman.net/doc/webman/install.html二,准备安装环境:1,需求环境需求PHP>=7.2Composer >=2.02,查看本地环境:php:liuhongdi@lhdpc:~$/usr/local/soft/php8/bin/php--version......
  • webman:修改默认页面(v1.5.7)
    一,默认页面的内容:说明:代码位于app/IndexController.php参考这个文档:https://www.workerman.net/doc/webman/route.html原始代码:显示了README.md这个文件的内容12345678910classIndexController{    publicfunctionindex(Request$reques......
  • webman:管理命令(v1.5.7)
     一,启动和停止1,启动#-d:以daemon方式启动,用于生产环境liuhongdi@lhdpc:/data/webman/imageadmin$phpstart.phpstart-dWorkerman[start.php]startinDAEMONmode-------------------------------------------WORKERMAN--------------------------------------......