DB9*_*B93 12 php validation laravel php-5.6 laravel-5
升级到Laravel 5.2后,我遇到了laravel验证器的问题.当我想验证控制器中的数据时,例如使用此代码.
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class ContactController extends Controller
{
public function storeContactRequest(Request $request)
{
$this->validate($request, [
'_token' => 'required',
'firstname' => 'required|string'
'lastname' => 'required|string'
'age' => 'required|integer',
'message' => 'required|string'
]);
// Here to store the message.
}
}
Run Code Online (Sandbox Code Playgroud)
但不知何故,当我输入无效数据时,它不会将我重定向回上一页并向会话中闪现一些消息,但它会触发异常并给我一个500错误页面.
这是我得到的例外.我在文档中读到ValidationException是新的而不是HttpResponseException但我不知道它是否与此有关.
[2016-01-05 11:49:49] production.ERROR: exception 'Illuminate\Foundation\Validation\ValidationException' with message 'The given data failed to pass validation.' in /home/vagrant/Code/twentyre-webshop/vendor/laravel/framework/src/Illuminate/Foundation/Validation/ValidatesRequests.php:70
Run Code Online (Sandbox Code Playgroud)
当我使用一个单独的请求类时,它只会重定向回错误消息.在我看来,只有控制器中使用的验证方法才会受到此行为的影响.
更新你的App\Exceptions\Handler课程
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Foundation\Validation\ValidationException;
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
AuthorizationException::class,
HttpException::class,
ModelNotFoundException::class,
ValidationException::class,
];
Run Code Online (Sandbox Code Playgroud)
我还建议您阅读如何迁移到laravel 5.2的文档,因为有一些重大更改.例如,这个ValidatesRequests特性抛出
Illuminate\Foundation\Validation\ValidationException而不是Illuminate\Http\Exception\HttpResponseException
Rob*_*bie -4
我在将 4.2 升级到 5.3 时遇到了同样的问题。
这个答案对我有用。
重写 app/Exceptions/Handler.php 中的方法
protected function convertExceptionToResponse(Exception $e)
{
if (config('app.debug')) {
$whoops = new \Whoops\Run;
$whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler);
return response()->make(
$whoops->handleException($e),
method_exists($e, 'getStatusCode') ? $e->getStatusCode() : 500,
method_exists($e, 'getHeaders') ? $e->getHeaders() : []
);
}
return parent::convertExceptionToResponse($e);
}
Run Code Online (Sandbox Code Playgroud)
答案在这里找到:https://laracasts.com/discuss/channels/laravel/whoops-20-laravel-52
| 归档时间: |
|
| 查看次数: |
13379 次 |
| 最近记录: |