bzilee / query-request
请求验证URL参数
1.2.2
2022-08-25 15:37 UTC
Requires
- php: ^7.3|^8.0
README
请求验证URL参数
工作原理
假设你有一个这样的URL www.bzilee.me/portfolio?post_id=1,你应该按照Laravel的formRequest的方式验证这个参数。问题是laravel formRequest只处理POST请求的数据。
这个包完成了GET请求的formRequests动作。
只需创建一个与formRequest继承者完全相同的验证文件。
并利用laravel的依赖注入来享受formRequest的相同功能。
条件
该包需要PHP 7.0或更高版本。Laravel包也需要Laravel 5.5或更高版本。
安装
通过以下步骤获取该包
$ composer require bzilee/query-request
$ composer dump-autoload
用法
<?php /** * QueryRequest Class */ namespace App\Http\Requests; use Bzilee\Abstracts\QueryRequest; class RetrievePostRequest extend QueryRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Pre Validation Function * * @return */ protected function prepareForValidation() { $this->merge([ // Another query to merge for before validation ]); } /** * Get the error messages for the defined validation rules. * * @return array */ public function messages() { return [ "post_id.required" => "The :attribute is required", ]; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ "post_id" => "required|numeric|exists:posts,id,deleted_at,NULL", ]; } /** * If validator fails return the exception in json form * * @return ResponseException */ public function failedValidation() { // your logic code here // example throw new \Exception($this->errors()); } /** * */ protected function failedAuthorization() { // your logic code here throw new \Exception("UnAuthorization"); } }
<?php /** * QueryRequest Class */ namespace App\Http\Controllers; use App\Http\Requests\RetrievePostRequest; class PostController extend Controller { /** * Display a listing of the resource. * * @param RetrievePostRequest $request */ protected function index(RetrievePostRequest $request) { $data = $request->validated(); // your logic code here } }
贡献
感谢您考虑为Laravel框架做出贡献!贡献指南可在Laravel文档中找到。
许可证
Laravel框架是开源软件,根据MIT许可证授权。
漏洞
如有安全疑虑,请告知。
许可证
MIT - 许可证