Skip to content

Commit d0c30f2

Browse files
committed
Update composer dependencies and refactor validation in controllers
- Added new validation packages: hyperf/validation and hyperf/translation. - Updated existing dependencies in composer.lock. - Refactored ProductController to use RequestValidation instead of Validation. - Updated ProductValidator to extend FormRequest and defined validation rules and messages. - Changed the error handling mode in TestController. - Added a new translation configuration file. - Enhanced hyperf.php with strict types and timezone settings.
1 parent abff9b9 commit d0c30f2

7 files changed

Lines changed: 1328 additions & 641 deletions

File tree

app/Controller/ProductController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use HPlus\Route\Annotation\ApiController;
99
use HPlus\Route\Annotation\PostApi;
1010
use HPlus\Route\Annotation\PutApi;
11-
use HPlus\Validate\Annotations\Validation;
11+
use HPlus\Validate\Annotations\RequestValidation;
1212
use Hyperf\HttpServer\Contract\RequestInterface;
1313
use Hyperf\HttpServer\Contract\ResponseInterface;
1414

@@ -22,7 +22,7 @@ class ProductController extends AbstractController
2222
* 创建产品
2323
*/
2424
#[PostApi(path: "", summary: "创建产品")]
25-
#[Validation(validate: ProductValidator::class, scene: "create")]
25+
#[RequestValidation(validate: ProductValidator::class, scene: "create")]
2626
public function store(RequestInterface $request)
2727
{
2828
$data = $request->all();
@@ -38,7 +38,7 @@ public function store(RequestInterface $request)
3838
* 更新产品
3939
*/
4040
#[PutApi(path: "{id}", summary: "更新产品")]
41-
#[Validation(validate: ProductValidator::class, scene: "update")]
41+
#[RequestValidation(validate: ProductValidator::class, scene: "update")]
4242
public function update(int $id, RequestInterface $request)
4343
{
4444
return [
@@ -51,7 +51,7 @@ public function update(int $id, RequestInterface $request)
5151
* 导入产品
5252
*/
5353
#[PostApi(path: "import", summary: "批量导入产品")]
54-
#[Validation(validate: ProductValidator::class, scene: "import")]
54+
#[RequestValidation(validate: ProductValidator::class, scene: "import")]
5555
public function import(RequestInterface $request)
5656
{
5757
$products = $request->input('products', []);

app/Controller/TestController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public function getTest(): array
192192
'email|邮箱' => 'required|email',
193193
'age|年龄' => 'integer|min:1|max:120'
194194
],
195-
dateType: 'json'
195+
mode: 'json'
196196
)]
197197
public function postTest(): array
198198
{
@@ -211,7 +211,7 @@ public function postTest(): array
211211
'username|用户名' => 'required|string|max:20',
212212
'password|密码' => 'required|string|min:6'
213213
],
214-
dateType: 'form'
214+
mode: 'form'
215215
)]
216216
public function formTest(): array
217217
{

app/Validator/ProductValidator.php

Lines changed: 54 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,74 @@
44

55
namespace App\Validator;
66

7-
use HPlus\Validate\Validate;
7+
use Hyperf\Validation\Request\FormRequest;
88

99
/**
10-
* 产品验证器
10+
* 产品验证器(Hyperf 风格)
1111
*/
12-
class ProductValidator extends Validate
12+
class ProductValidator extends FormRequest
1313
{
14-
protected $rule = [
15-
'name' => 'required|string|between:2,100',
16-
'description' => 'string|max:500',
17-
'price' => 'required|numeric|min:0',
18-
'stock' => 'required|integer|min:0',
19-
'category_id' => 'required|integer|min:1',
20-
'sku' => 'required|string|max:50|unique:products',
21-
'images' => 'array|max:10',
22-
'images.*' => 'url',
23-
'status' => 'integer|in:0,1'
24-
];
25-
26-
protected $message = [
27-
'name.required' => '产品名称不能为空',
28-
'name.between' => '产品名称长度必须在2-100个字符之间',
29-
'price.required' => '产品价格不能为空',
30-
'price.numeric' => '产品价格必须是数字',
31-
'price.min' => '产品价格不能小于0',
32-
'stock.required' => '库存数量不能为空',
33-
'stock.integer' => '库存数量必须是整数',
34-
'stock.min' => '库存数量不能小于0',
35-
'sku.unique' => 'SKU已存在',
36-
'images.max' => '产品图片最多10张'
14+
/**
15+
* 场景定义
16+
*/
17+
protected array $scenes = [
18+
'create' => ['name', 'description', 'price', 'stock', 'category_id', 'sku', 'images', 'status'],
19+
'update' => ['name', 'description', 'price', 'stock', 'category_id', 'images', 'status'],
20+
'import' => ['products'],
3721
];
38-
22+
3923
/**
40-
* 创建场景
24+
* 验证规则
4125
*/
42-
protected function sceneCreate()
26+
public function rules(): array
4327
{
44-
return $this->only(['name', 'description', 'price', 'stock', 'category_id', 'sku', 'images', 'status']);
28+
return [
29+
'name' => 'required|string|between:2,100',
30+
'description' => 'string|max:500',
31+
'price' => 'required|numeric|min:0',
32+
'stock' => 'required|integer|min:0',
33+
'category_id' => 'required|integer|min:1',
34+
'sku' => 'required|string|max:50',
35+
'images' => 'array|max:10',
36+
'images.*' => 'url',
37+
'status' => 'integer|in:0,1',
38+
'products' => 'required|array|min:1|max:1000',
39+
'products.*.name' => 'required|string|between:2,100',
40+
'products.*.price' => 'required|numeric|min:0',
41+
'products.*.stock' => 'required|integer|min:0',
42+
'products.*.sku' => 'required|string|max:50',
43+
];
4544
}
46-
45+
4746
/**
48-
* 更新场景
47+
* 错误消息
4948
*/
50-
protected function sceneUpdate()
49+
public function messages(): array
5150
{
52-
return $this->remove('sku', 'unique')
53-
->only(['name', 'description', 'price', 'stock', 'category_id', 'sku', 'images', 'status']);
51+
return [
52+
'name.required' => '产品名称不能为空',
53+
'name.between' => '产品名称长度必须在2-100个字符之间',
54+
'price.required' => '产品价格不能为空',
55+
'price.numeric' => '产品价格必须是数字',
56+
'price.min' => '产品价格不能小于0',
57+
'stock.required' => '库存数量不能为空',
58+
'stock.integer' => '库存数量必须是整数',
59+
'stock.min' => '库存数量不能小于0',
60+
'images.max' => '产品图片最多10张',
61+
];
5462
}
55-
63+
5664
/**
57-
* 导入场景
65+
* 字段别名
5866
*/
59-
protected function sceneImport()
67+
public function attributes(): array
6068
{
61-
return $this->only(['products'])
62-
->rule('products', 'required|array|min:1|max:1000')
63-
->rule('products.*.name', 'required|string|between:2,100')
64-
->rule('products.*.price', 'required|numeric|min:0')
65-
->rule('products.*.stock', 'required|integer|min:0')
66-
->rule('products.*.sku', 'required|string|max:50');
69+
return [
70+
'name' => '产品名称',
71+
'price' => '价格',
72+
'stock' => '库存',
73+
'category_id' => '分类',
74+
'sku' => 'SKU',
75+
];
6776
}
68-
}
77+
}

bin/hyperf.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env php
22
<?php
3+
4+
declare(strict_types=1);
35
/**
46
* This file is part of Hyperf.
57
*
@@ -8,23 +10,30 @@
810
* @contact group@hyperf.io
911
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
1012
*/
13+
use Hyperf\Contract\ApplicationInterface;
14+
use Hyperf\Di\ClassLoader;
15+
use Hyperf\Engine\DefaultOption;
16+
use Psr\Container\ContainerInterface;
17+
1118
ini_set('display_errors', 'on');
1219
ini_set('display_startup_errors', 'on');
1320
ini_set('memory_limit', '1G');
1421

1522
error_reporting(E_ALL);
23+
date_default_timezone_set('Asia/Shanghai');
1624

1725
! defined('BASE_PATH') && define('BASE_PATH', dirname(__DIR__, 1));
18-
! defined('SWOOLE_HOOK_FLAGS') && define('SWOOLE_HOOK_FLAGS', SWOOLE_HOOK_ALL);
1926

2027
require BASE_PATH . '/vendor/autoload.php';
2128

29+
! defined('SWOOLE_HOOK_FLAGS') && define('SWOOLE_HOOK_FLAGS', DefaultOption::hookFlags());
30+
2231
// Self-called anonymous function that creates its own scope and keep the global namespace clean.
2332
(function () {
24-
Hyperf\Di\ClassLoader::init();
25-
/** @var Psr\Container\ContainerInterface $container */
33+
ClassLoader::init();
34+
/** @var ContainerInterface $container */
2635
$container = require BASE_PATH . '/config/container.php';
2736

28-
$application = $container->get(Hyperf\Contract\ApplicationInterface::class);
37+
$application = $container->get(ApplicationInterface::class);
2938
$application->run();
3039
})();

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
"hyperf/redis": "^3.1",
2424
"hyperf-plus/route": "@dev",
2525
"hyperf-plus/swagger": "@dev",
26-
"hyperf-plus/validate": "@dev"
26+
"hyperf-plus/validate": "@dev",
27+
"hyperf/validation": "^3.1",
28+
"hyperf/translation": "^3.1"
2729
},
2830
"require-dev": {
2931
"friendsofphp/php-cs-fixer": "^3.0",

0 commit comments

Comments
 (0)