thinkphp6: 使用业务逻辑层(php 8.1.1 / thinkphp v6.0.10LTS )
一,从命令行创建用到的类:
liuhongdi@lhdpc:/data/php/admapi$ php think make:controller Goods Controller:app\controller\Goods created successfully.
liuhongdi@lhdpc:/data/php/admapi$ php think make:validate Goods/Detail Validate:app\validate\Goods\Detail created successfully.
liuhongdi@lhdpc:/data/php/admapi$ php think make:model Goods Model:app\model\Goods created successfully.
说明:刘宏缔的架构森林是一个专注架构的博客,
网站:https://blog.imgtouch.com
本文: https://blog.imgtouch.com/index.php/2023/05/29/thinkphp6-shi-yong-ye-wu-luo-ji-ceng-php-8-1-1-thinkphp-v6/
对应的源码可以访问这里获取: https://github.com/liuhongdi/
或: https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,配置:
1,数据库:
CREATE TABLE `goods` ( `goodsId` bigint NOT NULL AUTO_INCREMENT COMMENT 'id', `goodsName` varchar(300) NOT NULL DEFAULT '' COMMENT '商品名', `price` decimal(15,2) NOT NULL DEFAULT '0.00' COMMENT '价格', `stock` int NOT NULL DEFAULT '0' COMMENT '库存', PRIMARY KEY (`goodsId`) ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='商品表' INSERT INTO `goods` (`goodsId`, `goodsName`, `price`, `stock`) VALUES (12323, '条纹亮面行李箱 | 德国拜尔PC材质 静音轮', '1098.00', 6);
2, .env
[GOODSIMAGE] GOODS_IMAGE_DIR=/var/www/html/goodsimage GOODS_IMAGE_HOST=http://192.168.219.6/goodsimage
3,config/images.php
<?php return [ "goodsImageDir"=>env('goodsimage.goods_image_dir' ), "goodsImageHost"=>env('goodsimage.goods_image_host'), ];
三,编写php代码:
1,controller/Goods.php
<?php declare (strict_types = 1); namespace app\controller; use app\BaseController; use app\result\Result; use app\validate\Goods\Detail as GoodsDetailValidate; use app\business\Goods as goodsBusiness; use think\exception\ValidateException; use think\Request; class Goods extends BaseController { /** * 商品详情 * * @return \think\Response */ public function Detail(){ try { //echo "begin check:<br/>"; validate(GoodsDetailValidate::class) //->scene('edit') ->check($_GET); } catch (ValidateException $e) { // 验证失败 输出错误信息 //echo "get error:<br/>"; //var_dump($e->getError()); return Result::Error(422,$e->getError()); } $goodsId = $this->request->param('goodsid'); $goods = new goodsBusiness(); $res = $goods->getOneGoods($goodsId); if ($res['code'] == 0){ return Result::Success($res['data']); } else { return Result::Error(1,$res['msg']); } } }
2,business/Goods.php
<?php namespace app\business; use app\model\Goods as GoodsModel; use app\file\Goods as GoodsFile; class Goods { public function getOneGoods($goodId) { $goodsModel = new GoodsModel(); $goods = $goodsModel->getOneGoodsById($goodId); if (is_null($goods)) { return ['code'=>1,'msg'=>"数据错误"]; } else { $goodsFile = new GoodsFile(); $goodsUrl = $goodsFile->getImgUrl($goodId); $goods['goodsUrl'] = $goodsUrl; return ['code'=>0,'data'=>$goods]; } } }
3,model/Goods.php
<?php declare (strict_types = 1); namespace app\model; use think\facade\Db; use think\Model; /** * @mixin \think\Model */ class Goods extends Model { //类名与表名不一致时在这里指定数据表名 protected $table = "goods"; public function getOneGoodsById($goodsId){ //查询一条记录时用find方法 $result = Db::table("goods")->where("goodsId",$goodsId)->find(); return $result; } }
4,validate/Goods/Detail.php
<?php declare (strict_types = 1); namespace app\validate\Goods; use think\Validate; class Detail extends Validate { /** * 定义验证规则 * 格式:'字段名' => ['规则1','规则2'...] * * @var array */ protected $rule = [ 'goodsid' => 'require|integer|gt:0', ]; /** * 定义错误信息 * 格式:'字段名.规则名' => '错误信息' * * @var array */ protected $message = [ 'goodsid.require' => '商品id必须', 'goodsid.integer' => '商品id需要是整数', 'goodsid.gt' => '商品id需大于0', ]; }
5,file/Goods.php
<?php namespace app\file; use \think\facade\Config as GConfig; class Goods { public function getImgUrl($goodsId) { //$images = GConfig::get('images'); $host = GConfig::get('images.goodsImageHost'); $subDir = $goodsId - ($goodsId % 1000); $imgUrl = $host."/".$subDir."/".$goodsId.".jpg"; return $imgUrl; } }
6,result/Result.php
<?php namespace app\result; use think\response\Json; class Result { //success static public function Success($data):Json { $rs = [ 'code'=>0, 'msg'=>"success", 'data'=>$data, ]; return json($rs); } //error static public function Error($code,$msg):Json { $rs = [ 'code'=>$code, 'msg'=>$msg, 'data'=>"", ]; return json($rs); } }
7,config/images.php
<?php return [ "goodsImageDir"=>env('goodsimage.goods_image_dir' ), "goodsImageHost"=>env('goodsimage.goods_image_host'), ];
四,测试效果
1,访问存在的数据:
http://192.168.219.6:8000/goods/detail?goodsid=12323
返回:
2,访问不存在的数据:
http://192.168.219.6:8000/goods/detail?goodsid=12324
返回:
3,参数不规范:
http://192.168.219.6:8000/goods/detail
返回:
五,参看php和thinkphp的版本:
php:
liuhongdi@lhdpc:/data/php/admapi$ php --version PHP 8.1.1 (cli) (built: Dec 20 2021 16:12:16) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.1, Copyright (c) Zend Technologies with Zend OPcache v8.1.1, Copyright (c), by Zend Technologies
thinkphp:
liuhongdi@lhdpc:/var/www/html$ cd /data/php/admapi/ liuhongdi@lhdpc:/data/php/admapi$ php think version v6.0.10LTS