国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

webman request

v1.0.4 版本
2025-05-10 版本更新時間
43 安裝
2 star

Webman框架請求驗證器擴展包

本擴展包為Webman框架提供了一個優(yōu)雅的請求驗證解決方案,內(nèi)置了ThinkPHP驗證器,通過依賴注入方式自動生成請求對象,并對對象的字段進行校驗。

安裝

通過Composer安裝:

composer require hollisho/webman-request

基本使用

定義請求類

首先,創(chuàng)建一個繼承自WebmanRequest的請求類:

<?php

namespace app\request;

use Hollisho\WebmanRequest\WebmanRequest;

class MyRequest extends WebmanRequest
{
    // 定義請求參數(shù)屬性
    public $id;
    public $status = 0; // 可以設(shè)置默認(rèn)值

    // 定義驗證規(guī)則
    public function rules()
    {
        return [
            'id' => 'require|number',
            'status' => 'require|number',
        ];
    }

    // 定義錯誤消息
    protected function messages()
    {
        return [
            'id.require' => 'id不能為空',
            'id.number' => 'id必須為數(shù)字',
            'status.require' => 'status不能為空',
            'status.number' => 'status必須為數(shù)字',
        ];
    }
}

在控制器中使用

在控制器中通過依賴注入方式使用請求類:

<?php

namespace app\controller;

use app\request\MyRequest;
use support\Response;

class IndexController
{
    public function index(MyRequest $request)
    {
        // 驗證通過后,可以直接使用請求對象的屬性
        $id = $request->id;
        $status = $request->status;

        // 業(yè)務(wù)邏輯處理...
        return json(['code' => 0, 'msg' => 'success', 'data' => [
            'id' => $id,
            'status' => $status
        ]]);
    }
}

常用驗證規(guī)則

本擴展包內(nèi)置了ThinkPHP驗證器的所有驗證規(guī)則,以下是一些常用的驗證規(guī)則:

規(guī)則 說明 示例
require 必須填寫 'name' => 'require'
number 必須是數(shù)字 'age' => 'number'
integer 必須是整數(shù) 'count' => 'integer'
float 必須是浮點數(shù) 'price' => 'float'
boolean 必須是布爾值 'status' => 'boolean'
email 必須是郵箱格式 'email' => 'email'
array 必須是數(shù)組 'tags' => 'array'
date 必須是日期格式 'birthday' => 'date'
alpha 必須是字母 'name' => 'alpha'
alphaNum 必須是字母和數(shù)字 'account' => 'alphaNum'
alphaDash 必須是字母、數(shù)字、下劃線或破折號 'username' => 'alphaDash'
chs 必須是中文 'name' => 'chs'
chsAlpha 必須是中文或字母 'name' => 'chsAlpha'
chsAlphaNum 必須是中文、字母或數(shù)字 'name' => 'chsAlphaNum'
chsDash 必須是中文、字母、數(shù)字、下劃線或破折號 'name' => 'chsDash'
url 必須是URL地址 'website' => 'url'
ip 必須是IP地址 'ip' => 'ip'
mobile 必須是手機號碼 'mobile' => 'mobile'
idCard 必須是身份證號碼 'idcard' => 'idCard'
zipCode 必須是郵政編碼 'zipcode' => 'zipCode'
in 必須在范圍內(nèi) 'type' => 'in:1,2,3'
notIn 必須不在范圍內(nèi) 'type' => 'notIn:1,2,3'
between 必須在范圍內(nèi) 'age' => 'between:18,60'
notBetween 必須不在范圍內(nèi) 'age' => 'notBetween:0,17'
length 長度必須在范圍內(nèi) 'name' => 'length:2,20'
max 最大長度 'name' => 'max:20'
min 最小長度 'password' => 'min:6'
after 必須在日期之后 'begin_time' => 'after:2020-01-01'
before 必須在日期之前 'end_time' => 'before:2030-01-01'
confirm 必須和指定字段相同 'repassword' => 'confirm:password'
different 必須和指定字段不同 'nickname' => 'different:username'
eq 必須等于指定值 'status' => 'eq:1'
neq 必須不等于指定值 'status' => 'neq:0'
gt 必須大于指定值 'age' => 'gt:18'
lt 必須小于指定值 'age' => 'lt:60'
egt 必須大于等于指定值 'age' => 'egt:18'
elt 必須小于等于指定值 'age' => 'elt:60'
regex 必須滿足正則表達式 'zip' => 'regex:/^\d{6}$/'

總結(jié)

hollisho/webman-request擴展包為Webman框架提供了一個強大而靈活的請求驗證解決方案,通過依賴注入的方式自動驗證請求參數(shù),使代碼更加簡潔和易于維護。它支持多種驗證規(guī)則、自定義驗證規(guī)則、驗證場景等高級功能,能夠滿足各種復(fù)雜的驗證需求。

參考鏈接

特別說明

特別說明

  • 由于thinkphp驗證器的原因,本擴展包不支持php8.0以上的版本,請在使用前確認(rèn)您的PHP版本。
  • 本擴展包僅提供了基本的請求驗證功能,對于更復(fù)雜的驗證需求,您可以根據(jù)實際情況自行擴展。(例如場景的支持目前尚未支持)
贊助商