dsxwk/hyperf-helper

There is no license information available for the latest version (v1.1.0) of this package.

Hyperf Framework Assistant

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/dsxwk/hyperf-helper

v1.1.0 2025-11-11 02:53 UTC

This package is not auto-updated.

Last update: 2025-11-11 02:56:17 UTC


README

安装

composer require dwxwk/hyperf-helper

验证器

继承BaseFormRequest类,实现sceneRule方法,返回验证场景规则, 支持每个场景自定义字段验证,满足更复杂验证。官方的公共rules方法,无法满足复杂验证场景。如创建某个字段必填,但是更新相同的字段又不需要验证或者使用其他验证,增加了灵活性。

<?php

declare(strict_types=1);

namespace App\Request;

use Dsxwk\Framework\HyperfHelper\Request\BaseFormRequest;

class IndexRequest extends BaseFormRequest
{
    /**
     * 验证场景规则
     *
     * @return array
     */
    public function sceneRules(): array
    {
        return [
            // 创建
            'create'               => [
                'remark'      => 'nullable|string',
                'attachments' => 'array',
                // ...
            ],
            // 更新
            'update'               => [
                'id'          => 'required|integer|gt:0',
                'remark'      => 'nullable|string',
                'attachments' => 'array',
                // ...
            ],
        ];
    }

    /**
     * 提示字段设置
     *
     * @return string[]
     */
    public function attributes(): array
    {
        return [
            'id'                         => 'ID',
            'remark'                     => '备注',
            'attachments'                => '附件',
        ];
    }
}

控制器

<?php

declare(strict_types=1);

namespace App\Controller;

use App\Request\IndexRequest;
use Hyperf\Di\Annotation\Inject;

class IndexController
{
    #[Inject]
    public IndexRequest $indexRequest;

    public function create()
    {
        // 验证场景
        $this->indexRequest->scene('create')->validateResolved();
        // ...
    }
    
    public function update()
    {
        // 验证场景
        $this->indexRequest->scene('update')->validateResolved();
        // ...
    }
}