在`laravel`中,每一个请求都会被封装为一个`request`对象,`form request`对象就是包含了额外验证逻辑(以及访问权限控制)的自定义`request`类。 本文分析了formrequest异常的处理流程并提出了自定义处理formrequest验证失败的思路。
所有示例基于laravel 5.1.39 (lts)
今天天气不错,我们来说说表单验证。
controller中做表单验证
有的同学把表单验证逻辑写在controller中,例如这个对用户提交评论内容的验证:
<?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();
}
}
这样写的话,表单验证和业务逻辑挤在一起,我们的controller中就会有太多的代码,而且重复的验证规则基本也是复制粘贴。
我们可以利用form request来封装表单验证代码,从而精简controller中的代码逻辑,使其专注于业务。而独立出去的表单验证逻辑甚至可以复用到其它请求中,例如修改评论。
什么是form request
在laravel中,每一个请求都会被封装为一个request对象,form request对象就是包含了额外验证逻辑(以及访问权限控制)的自定义request类。
如何使用form request做表单验证
laravel提供了生成form request的artisan命令:
<code>$ php artisan make:request storecommentrequest</code>
于是就生成了app/http/requests/storecommentrequest.php,让我们来分析一下内容:
<?php
namespace apphttprequests;
use apphttprequestsrequest; // 可以看到,这个基类是在我们的项目中的,这意味着我们可以修改它
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 [
//
];
}
}
那么很容易,我们除了让authorize方法返回true之外,还得让rules方法返回我们的验证规则:
<?php
// ...
public function rules()
{
return [
];
}
// ...
接着修改我们的controller:
<?php
// ...
// 之前:public function poststorecomment(request $request)
public function poststorecomment(apphttprequestsstorecommentrequest $request)
{
// ...
}
// ...
这样laravel便会自动调用storecommentrequest进行表单验证了。
异常处理
如果表单验证失败,laravel会重定向到之前的页面,并且将错误写到session中,如果是ajax请求,则会返回一段http状态为422的json数据,类似这样:
<code>{comment: ["the comment field is required."]}</code>
这里就不细说提示信息怎么修改了,如果有人想看相关教程,可以留言。
我们主要来说说怎么定制错误处理。
通常来说,laravel中的错误都是异常(exception),我们都可以在appexceptionshandler.php中进行统一处理。form request确实也抛出了一个illuminatehttpexceptionhttpresponseexception异常,但这个异常是在路由逻辑中就被特殊处理了。
首先我们来看看form request是如何被执行的:
illuminatevalidationvalidationserviceprovider:
<?php
namespace illuminatevalidation;
use illuminatesupportserviceprovider;
use illuminatecontractsvalidationvalidateswhenresolved;
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`方法进行验证
});
}
// ...
你猜对了,form request就实现了这个illuminatecontractsvalidationvalidateswhenresolved接口:
<?php
namespace illuminatefoundationhttp;
use illuminatehttprequest;
use illuminatehttpresponse;
use illuminatehttpjsonresponse;
use illuminateroutingredirector;
use illuminatecontainercontainer;
use illuminatecontractsvalidationvalidator;
use illuminatehttpexceptionhttpresponseexception;
use illuminatevalidationvalidateswhenresolvedtrait;
use illuminatecontractsvalidationvalidateswhenresolved; // 是你
use illuminatecontractsvalidationfactory as validationfactory;
// 我们`apphttprequestsrequest`便是继承于这个`formrequest`类
class formrequest extends request implements validateswhenresolved // 就是你
{
use validateswhenresolvedtrait; // 这个我们待会儿也要看看
// ...
formrequest基类中的validate方法是由这个illuminatevalidationvalidateswhenresolvedtrait实现的:
illuminatevalidationvalidateswhenresolvedtrait:
<?php
namespace illuminatevalidation;
use illuminatecontractsvalidationvalidationexception;
use illuminatecontractsvalidationunauthorizedexception;
/**
* 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); // 这里调用了验证失败的处理,我们主要看这里
}
}
// ...
在validate里,如果验证失败了就会调用$this->failedvalidation(),继续:
illuminatefoundationhttpformrequest:
<?php
// ...
/**
* handle a failed validation attempt.
*
* @param illuminatecontractsvalidationvalidator $validator
* @return mixed
*/
protected function failedvalidation(validator $validator)
{
throw new httpresponseexception($this->response( // 这里抛出了传说中的异常
$this->formaterrors($validator)
));
}
终于看到异常了!可是这个异常在另一个地方被处理了:
illuminateroutingroute:
<?php
// ...
/**
* run the route action and return the response.
*
* @param illuminatehttprequest $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给客户端
}
}
// ...
至此,整个思路已然清晰,不过我们还是看看这里生成的httpresponseexception异常中的response是怎么生成的:
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);
// ...
相信你都看明白了。
如何实现自定义错误处理,这里提供两个思路,都需要重写apphttprequestsrequest的failedvalidation:
抛出一个新异常,继承httpresponseexception异常,重新实现getresponse方法,这个异常类我们可以放到app/exceptions/下便于管理,错误返回依然交给laravel;
抛出一个我们自定义的异常,在appexceptionshandler中处理。
具体实现这里就不写啦(参阅laravel文档中关于错误处理部分,中文文档传送门),如果你有别的方法或者想法可以在评论中和我交流。
补充
如果你的controller使用illuminatefoundationvalidationvalidatesrequests这个trait的validate方法进行验证,同样的,这里验证失败也会抛出illuminatehttpexceptionhttpresponseexception异常,可以参考上面的解决方案进行处理。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!