


A summary of form verification methods and problems using FormRequest in Laravel, laravelformrequest
In `Laravel`, each request will be encapsulated into a `Request` object, `Form Request `Objects are custom `Request` classes that contain additional validation logic (and access control). This article analyzes the FormRequest exception handling process and proposes a custom way to handle FormRequest verification failure.
All examples are based on Laravel 5.1.39 (LTS)
The weather is nice today, let’s talk about form validation.
Do form validation in Controller
Some students write form verification logic in the Controller, such as this verification of user-submitted comment content:
<?php // ... use Validator; class CommentController { public function postStoreComment(Request $request) { $validator = Validator::make($request->all(), [ 'comment' => 'required', // 只是实例,就写个简单的规则,你的网站要是这么写欢迎在评论里贴网址 ]); if ($validator->fails()) { return redirect() ->back() ->withErrors($validator) ->withInput(); } }
If written like this, form validation and business logic will be squeezed together, there will be too much code in our Controller, and repeated validation rules will basically be copied and pasted.
We can use Form Request to encapsulate the form validation code, thereby streamlining the code logic in the Controller and focusing it on the business. The independent form validation logic can even be reused in other requests, such as modifying comments.
What is Form Request
In Laravel, each request will be encapsulated as a Request object. The Form Request object is a custom Request class that contains additional verification logic (and access control).
How to use Form Request for form verification
Laravel provides Artisan commands to generate Form Request:
$ php artisan make:request StoreCommentRequest
So app/Http/Requests/StoreCommentRequest.php was generated, let us analyze the content:
<?php namespace App\Http\Requests; use App\Http\Requests\Request; // 可以看到,这个基类是在我们的项目中的,这意味着我们可以修改它 class StoreCommentRequest extends Request { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() // 这个方法可以用来控制访问权限,例如禁止未付费用户评论… { return false; // 注意!这里默认是false,记得改成true } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() // 这个方法返回验证规则数组,也就是Validator的验证规则 { return [ // ]; } }
Then it’s easy. In addition to letting the authorize method return true, we also have to let the rules method return our verification rules:
<?php // ... public function rules() { return [ ]; } // ...
Then modify our Controller:
<?php // ... // 之前:public function postStoreComment(Request $request) public function postStoreComment(\App\Http\Requests\StoreCommentRequest $request) { // ... } // ...
In this way, Laravel will automatically call StoreCommentRequest for form verification.
Exception handling
If the form validation fails, Laravel will redirect to the previous page and write the error to the Session. If it is an AJAX request, a piece of JSON data with HTTP status 422 will be returned, similar to this:
{comment: ["The comment field is required."]}
I won’t go into details here on how to modify the prompt information. If anyone wants to see related tutorials, you can leave a message.
We mainly talk about how to customize error handling.
Generally speaking, errors in Laravel are exceptions, and we can all handle them uniformly in appExceptionshandler.php. Form Request did throw an IlluminateHttpExceptionHttpResponseException exception, but this exception was specially handled in the routing logic.
First let’s take a look at how Form Request is executed:
IlluminateValidationValidationServiceProvider:
<?php namespace Illuminate\Validation; use Illuminate\Support\ServiceProvider; use Illuminate\Contracts\Validation\ValidatesWhenResolved; class ValidationServiceProvider extends ServiceProvider { /** * Register the service provider. * * @return void */ public function register() { $this->registerValidationResolverHook(); // 看我看我看我 $this->registerPresenceVerifier(); $this->registerValidationFactory(); } /** * Register the "ValidatesWhenResolved" container hook. * * @return void */ protected function registerValidationResolverHook() // 对,就是我 { // 这里可以看到对`ValidatesWhenResolved`的实现做了一个监听 $this->app->afterResolving(function (ValidatesWhenResolved $resolved) { $resolved->validate(); // 然后调用了它的`validate`方法进行验证 }); } // ...
You guessed it, Form Request implements this IlluminateContractsValidationValidatesWhenResolved interface:
<?php namespace Illuminate\Foundation\Http; use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Http\JsonResponse; use Illuminate\Routing\Redirector; use Illuminate\Container\Container; use Illuminate\Contracts\Validation\Validator; use Illuminate\Http\Exception\HttpResponseException; use Illuminate\Validation\ValidatesWhenResolvedTrait; use Illuminate\Contracts\Validation\ValidatesWhenResolved; // 是你 use Illuminate\Contracts\Validation\Factory as ValidationFactory; // 我们`app\Http\Requests\Request`便是继承于这个`FormRequest`类 class FormRequest extends Request implements ValidatesWhenResolved // 就是你 { use ValidatesWhenResolvedTrait; // 这个我们待会儿也要看看 // ...
The validate method in the FormRequest base class is implemented by this IlluminateValidationValidatesWhenResolvedTrait:
IlluminateValidationValidatesWhenResolvedTrait:
<?php namespace Illuminate\Validation; use Illuminate\Contracts\Validation\ValidationException; use Illuminate\Contracts\Validation\UnauthorizedException; /** * Provides default implementation of ValidatesWhenResolved contract. */ trait ValidatesWhenResolvedTrait { /** * Validate the class instance. * * @return void */ public function validate() // 这里实现了`validate`方法 { $instance = $this->getValidatorInstance(); // 这里获取了`Validator`实例 if (! $this->passesAuthorization()) { $this->failedAuthorization(); // 这是调用了访问授权的失败处理 } elseif (! $instance->passes()) { $this->failedValidation($instance); // 这里调用了验证失败的处理,我们主要看这里 } } // ...
In validate, if the verification fails, $this->failedValidation() will be called, continue:
IlluminateFoundationHttpFormRequest:
<?php // ... /** * Handle a failed validation attempt. * * @param \Illuminate\Contracts\Validation\Validator $validator * @return mixed */ protected function failedValidation(Validator $validator) { throw new HttpResponseException($this->response( // 这里抛出了传说中的异常 $this->formatErrors($validator) )); }
Finally saw something unusual! But this exception was handled in another place:
IlluminateRoutingRoute:
<?php // ... /** * Run the route action and return the response. * * @param \Illuminate\Http\Request $request * @return mixed */ public function run(Request $request) { $this->container = $this->container ?: new Container; try { if (! is_string($this->action['uses'])) { return $this->runCallable($request); } if ($this->customDispatcherIsBound()) { return $this->runWithCustomDispatcher($request); } return $this->runController($request); } catch (HttpResponseException $e) { // 就是这里 return $e->getResponse(); // 这里直接返回了Response给客户端 } } // ...
At this point, the whole idea is clear, but let’s take a look at how the Response in the HttpResponseException generated here is generated:
IlluminateFoundationHttpFormRequest:
<?php // ... // 132行: if ($this->ajax() || $this->wantsJson()) { // 对AJAX请求的处理 return new JsonResponse($errors, 422); } return $this->redirector->to($this->getRedirectUrl()) // 对普通表单提交的处理 ->withInput($this->except($this->dontFlash)) ->withErrors($errors, $this->errorBag); // ...
I believe you understand everything.
How to implement custom error handling, here are two ideas, both of which need to rewrite the failedValidation of appHttpRequestsRequest:
Throw a new exception, inherit the HttpResponseException exception, and reimplement the getResponse method. We can put this exception class under app/Exceptions/ for easy management, and error returns are still handed over to Laravel;
Throws a custom exception and handles it in appExceptionshandler.
The specific implementation will not be written here (refer to the error handling section in the Laravel documentation, Chinese document portal). If you have other methods or ideas, you can communicate with me in the comments.
Supplement
If your Controller uses the validate method of the IlluminateFoundationValidationValidatesRequests Trait for verification, similarly, if the verification fails here, an IlluminateHttpExceptionHttpResponseException exception will be thrown. You can refer to the above solution for processing.

本篇文章给大家带来了关于laravel的相关知识,其中主要介绍了关于单点登录的相关问题,单点登录是指在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于laravel的相关知识,其中主要介绍了关于Laravel的生命周期相关问题,Laravel 的生命周期从public\index.php开始,从public\index.php结束,希望对大家有帮助。

在laravel中,guard是一个用于用户认证的插件;guard的作用就是处理认证判断每一个请求,从数据库中读取数据和用户输入的对比,调用是否登录过或者允许通过的,并且Guard能非常灵活的构建一套自己的认证体系。

laravel中asset()方法的用法:1、用于引入静态文件,语法为“src="{{asset(‘需要引入的文件路径’)}}"”;2、用于给当前请求的scheme前端资源生成一个url,语法为“$url = asset('前端资源')”。

本篇文章给大家带来了关于laravel的相关知识,其中主要介绍了关于使用中间件记录用户请求日志的相关问题,包括了创建中间件、注册中间件、记录用户访问等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于laravel的相关知识,其中主要介绍了关于中间件的相关问题,包括了什么是中间件、自定义中间件等等,中间件为过滤进入应用的 HTTP 请求提供了一套便利的机制,下面一起来看一下,希望对大家有帮助。

在laravel中,fill方法是一个给Eloquent实例赋值属性的方法,该方法可以理解为用于过滤前端传输过来的与模型中对应的多余字段;当调用该方法时,会先去检测当前Model的状态,根据fillable数组的设置,Model会处于不同的状态。

laravel路由文件在“routes”目录里。Laravel中所有的路由文件定义在routes目录下,它里面的内容会自动被框架加载;该目录下默认有四个路由文件用于给不同的入口使用:web.php、api.php、console.php等。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

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),
