raid / core-request
Raid 核心请求包
dev-main
2024-01-24 22:39 UTC
Requires
- php: ^8.2
- raid/core-command: dev-main
Requires (Dev)
- laravel/pint: ^1.10
This package is auto-updated.
Last update: 2024-09-25 00:05:41 UTC
README
此包负责处理系统中的所有请求。
安装
composer require raid/core-request
配置
php artisan core:publish-request
使用
use App\Traits\Request\WithUserCommonRules; use Raid\Core\Request\Requests\FormRequest; class CreateUserRequest extends FormRequest { use WithUserCommonRules; /** * Get the validation rules that apply to the request. */ public function rules(): array { return $this->withCommonRules(); } }
如何使用它
让我们从我们的请求类 CreateUserRequest
开始。
您可以使用命令来创建请求类。
php artisan core:make-request CreateUserRequest
<?php namespace App\Http\Requests; use Raid\Core\Request\Requests\FormRequest; class CreateUserRequest extends FormRequest { /** * Get the validation rules that apply to the request. */ public function rules(): array { return []; } }
这看起来像是一个正常的请求类,但实际上它只需要一些助手。
请求类必须扩展 FormRequest
类。
现在,让我们创建我们的 common-rules-request
特性。
您可以使用命令来创建公共请求特性。
php artisan core:make-common-request WithUserCommonRules
<?php namespace App\Traits\Request; trait WithUserCommonRules { /** * Get the common rules for the request. */ public function commonRules(): array { return []; } /** * Get the common attributes for the request. */ public function attributes(): array { return []; } }
commonRules
方法负责返回请求的公共规则。
您可以定义请求中常见规则及其属性。
<?php namespace App\Traits\Request; trait WithUserCommonRules { /** * Get the common rules for the user. */ public function commonRules(): array { return [ 'name' => ['string', 'min:2', 'max:255'], ]; } /** * Get the common attributes for the user. */ public function attributes(): array { return [ 'name' => __('attributes.name'), ]; } }
使用公共规则
现在让我们回到我们的请求类。
<?php namespace App\Http\Requests; use Raid\Core\Request\Requests\FormRequest; use App\Traits\Request\WithUserCommonRules; class CreateUserRequest extends FormRequest { use WithUserCommonRules; /** * Get the validation rules that apply to the request. */ public function rules(): array { return $this->withCommonRules([ 'name' => ['required'], ]); } }
<?php namespace App\Http\Requests; use Raid\Core\Request\Requests\FormRequest; use App\Traits\Request\WithUserCommonRules; class UpdateUserRequest extends FormRequest { use WithUserCommonRules; /** * Get the validation rules that apply to the request. */ public function rules(): array { return $this->withCommonRules([ 'name' => ['sometimes'], ]); } }
现在,两个请求都继承了所有定义的公共规则和属性,并且每个请求都有自己的规则。
withCommonRules
方法负责合并公共规则和请求规则。
withCommonRules
方法接受一个规则数组,并将其与公共规则合并。
请记住,所有公共规则都将由使用公共规则特性的所有请求继承。
仅使用公共规则
要仅将公共规则与请求规则合并,可以使用 withOnlyCommonRules
方法。
<?php namespace App\Http\Requests; use Raid\Core\Request\Requests\FormRequest; use App\Traits\Request\WithUserCommonRules; class CreateUserRequest extends FormRequest { use WithUserCommonRules; /** * Get the validation rules that apply to the request. */ public function rules(): array { return $this->withOnlyCommonRules([ 'name' => ['required'], ]); } }
withOnlyCommonRules
方法负责将公共规则与请求规则合并,并忽略所有其他规则。
这将只合并名称规则与公共规则中的名称规则,并忽略所有其他定义的公共规则。
就是这样。
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件。
致谢
安全
如果您发现任何与安全相关的问题,请通过电子邮件而不是使用问题跟踪器。
关于 Raid
Raid 是由 Mohamed Khedr 创建的 PHP 框架
并由 Mohamed Khedr 维护。
支持 Raid
Raid 是一个 MIT 许可的开源项目。这是一个独立项目,其持续开发得以实现。