


Analyze how to carry out Laravel form verification layered design and verification scenario application
思考
我发现大多数的Laravel用户会将验证规则写在Controller下,但这会有一个问题,使代码杂乱且无法复用相同的规则。在CRUD为主的项目中,我们会发现好多字段以及它们的验证规则是相同的,我们一般会化身为CV工程师,再复制一份,那么为什么不加一层验证层?
伴随这个问题,然后去看了别的开源项目的代码,发现TP用户的验证会放在一个单独的类中,并且有验证场景
来支持他们复用规则,于是我寻找Laravel
中有没有对应的解决方案,然后发现了微擎的一个验证器w7/engine-validate
,接下里的文章就依赖此扩展来讲如何增加一个验证层
安装
composer require w7/engine-validate
介绍
首先此验证器也是基于illuminate/validation
的,关于它的详细说明可以直接查看它的文档,这里就不过多说明了。
使用
首先我们建立一个和控制器相对应的验证层
app ├── Http │ ├── Controllers │ │ └── UserController.php │ └── Validate │ └── UserValidate.php
验证器
验证器代码:
class UserValidate extends Validate{ protected $rule = [ 'user' => 'required|email', 'pass' => 'required|alpha_num' ]; protected $message = [ 'user.required' => '账号不可为空', 'pass.required' => '密码不可为空', ]; protected $customAttributes = [ 'user' => '账号', 'pass' => '密码', ];}
控制器代码:
class UserController extends BaseController{ use AuthorizesRequests, DispatchesJobs, ValidatesRequests; public function login(Request $request) { $data = UserValidate::make()->check($request->all()); }}
这个时候,如果值不符合要求,会抛出一个ValidateException
异常,我们可以选择手动捕获,也可以选择在异常捕获类里面做一个全局的处理:
public function render($request, Throwable $e) { if ($e instanceof ValidateException) { return response()->json([ 'code' => -1, 'message' => $e->getMessage() ]); } return parent::render($request, $e); }
这样我们就不需要再控制器里面管理任何的验证异常了。
此时,控制器中的$data
一定是符合我们需求的数据,然后可以紧接着做对应的业务处理
验证场景
这个时候,如果我们的UserController
控制器中又添加了一个register
方法,在以上两个字段的需求同时增加了name
,应该如何处理呢?
我们可以使用类的$scene
属性来指定某一场景下需要验证的字段
验证器代码修改如下:
class UserValidate extends Validate{ protected $rule = [ 'user' => 'required|email', 'pass' => 'required|alpha_num', 'name' => 'required|alpha' ]; protected $message = [ 'user.required' => '账号不可为空', 'pass.required' => '密码不可为空', ]; protected $customAttributes = [ 'user' => '账号', 'pass' => '密码', 'name' => '用户名称', ]; protected $scene = [ 'login' => ['user', 'pass'], 'register' => ['user', 'pass', 'name'] ];}
此时:login
场景对应的验证user
和pass
字段,而register
场景对应的验证user
,pass
,name
字段
控制器代码修改如下:
class UserController extends BaseController{ use AuthorizesRequests, DispatchesJobs, ValidatesRequests; public function login(Request $request) { $data = UserValidate::make()->scene('login')->check($request->all()); } public function register(Request $request) { $data = UserValidate::make()->scene('register')->check($request->all()); }}
使用验证器的scene
方法来指定当前需要验证的场景名称
验证中间件
默认我们一个控制器对应一个验证器,一个方法对应一个场景名称,基于此特点,我们可以编写一些更为简易的方法来解决验证,我们可以编写一个中间件来解决此问题,点击查看文档说明,中间件完整命名空间为:Itwmw\Validate\Middleware\Laravel\ValidateMiddleware
中间件设置
首先我们需要为他指定控制器和验证器的对应关系,在app/Providers
目录下新建一个ValidateServiceProvider.php
文件,写入如下代码:
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Itwmw\Validate\Middleware\ValidateMiddlewareConfig; class ValidateServiceProvider extends ServiceProvider{ public function register() { ValidateMiddlewareConfig::instance() ->setAutoValidatePath('App\\Http\\Controllers\\', 'App\\Http\\Validate\\'); }}
其中setAutoValidatePath
为指定控制器和验证器的对应关系,可以设定多个,不限制数量,然后我们将ValidateServiceProvider
注册一下,在config/app.php
文件中找到providers
,在其中添加App\Providers\ValidateServiceProvider::class
注意:不可以放在
Illuminate\Validation\ValidationServiceProvider::class
之前
使用中间件
这个时候,我们可以将中间件注册为全局中间件,也可以不注册,注册方法:中间件《Laravel 7 中文文档》
定义路由:
Route::middleware(ValidateMiddleware::class)->group(function () { Route::any('/login', [\App\Http\Controllers\UserController::class, 'login']); Route::any('/register', [\App\Http\Controllers\UserController::class, 'register']);});
控制器代码修改如下:
class UserController extends BaseController{ use AuthorizesRequests, DispatchesJobs, ValidatesRequests; public function login(Request $request) { $data = get_validate_data($request); } public function register(Request $request) { $data = get_validate_data($request); }}
这个时候,我们就不需要手动指定验证器已经场景名了,中间件会自动处理对应的验证规则,我们只需要使用get_validate_data
方法来接收值即可。
结尾
此文章只写了这个扩展的一些基本用法,还有更多的功能,需要大家自己看看文档,来完成自己合适的验证层。
For more laravel technical articles, please visit the laravel tutorial column!
The above is the detailed content of Analyze how to carry out Laravel form verification layered design and verification scenario application. For more information, please follow other related articles on the PHP Chinese website!

Laravelsoftdeletesallow"deletion"withoutremovingrecordsfromthedatabase.Toimplement:1)UsetheSoftDeletestraitinyourmodel.2)UsewithTrashed()toincludesoft-deletedrecordsinqueries.3)CreatecustomscopeslikeonlyTrashed()forstreamlinedcode.4)Impleme

In Laravel, restore the soft deleted records using the restore() method, and permanently delete the forceDelete() method. 1) Use withTrashed()->find()->restore() to restore a single record, and use onlyTrashed()->restore() to restore a single record. 2) Permanently delete a single record using withTrashed()->find()->forceDelete(), and multiple records use onlyTrashed()->forceDelete().

You should download and upgrade to the latest Laravel version as it provides enhanced EloquentORM capabilities and new routing features, which can improve application efficiency and security. To upgrade, follow these steps: 1. Back up the current application, 2. Update the composer.json file to the latest version, 3. Run the update command. While some common problems may be encountered, such as discarded functions and package compatibility, these issues can be solved through reference documentation and community support.

YoushouldupdatetothelatestLaravelversionwhenthebenefitsclearlyoutweighthecosts.1)Newfeaturesandimprovementscanenhanceyourapplication.2)Securityupdatesarecrucialifvulnerabilitiesareaddressed.3)Performancegainsmayjustifyanupdateifyourappstruggles.4)Ens

Laravel'ssoftdeletefeaturecanbeusedeffectivelytosafeguarddatabyfollowingspecificstepsandbestpractices.1)ImplementsoftdeletesusingtheSoftDeletestraitinyourmodel.2)UsewithTrashed()toquerysoft-deletedrecordsforauditingorrecovery.3)UseforceDelete()cautio

Developers can efficiently track new versions of Laravel and ensure the use of the latest and safest code bases: 1. Use code snippets to check the latest version and compare it with the current version, 2. Use Composer and Laravel for dependency management, 3. Implement automated testing to deal with version conflicts, 4. Get feedback on new versions through community interaction, 5. Pay attention to Laravel's public roadmap and GitHub dynamics to plan updates.

Laravel's latest version (9.x) brings important security updates, including: 1) patching known vulnerabilities such as CSRF attacks; 2) enhancing overall security, such as CSRF protection and SQL injection defense. By understanding and applying these updates correctly, you can ensure that your Laravel app is always in the safest state.

LaravelMigrationsareversioncontrolfordatabases,allowingschemamanagementandevolution.1)Theyhelpmaintainteamsyncandconsistencyacrossenvironments.2)Usethemtocreatetableslikethe'users'tablewithnecessaryfields.3)Modifyexistingtablesbyaddingfieldslike'phon


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
