Laravel 5.2 Validator Throuing异常,而不是在调试模式关闭时重定向

时间:2016-04-20 13:08:37

标签: laravel laravel-5.2

我将我的网站升级到Laravel 5.2现在,如果我的用户不在字段中输入任何值而不是将其重定向回上一个和flash错误消息,则显示异常。以下是我的示例代码。

public function save(Request $request)
{
    $this->validate($request, [
        'title' => 'required',
        'slug' => 'required'
    ]);

}

我尝试在App\Exceptions\Handler课程中添加以下行,但仍无效。

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Foundation\Validation\ValidationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler {
    /**
     * A list of the exception types that should not be reported.
     *
     * @var array
     */
    protected $dontReport = [
        AuthorizationException::class,
        HttpException::class,
        ModelNotFoundException::class,
        ValidationException::class,
    ];

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  \Exception  $e
     * @return void
     */
    public function report(Exception $e) {
        return parent::report($e);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $e
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $e) {

        if (!config('app.debug') && !$this->isHttpException($e)) {

                return redirect('404');
        }

        return parent::render($request, $e);
    }
}

1 个答案:

答案 0 :(得分:0)

您需要确保在渲染方法中没有捕获HttpResponseExceptions。

试试这个:     

namespace App\Exceptions;

use Exception;
use Illuminate\Validation\ValidationException;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Exception\HttpResponseException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler {
    /**
     * A list of the exception types that should not be reported.
     *
     * @var array
     */
    protected $dontReport = [
        AuthorizationException::class,
        HttpException::class,
        ModelNotFoundException::class,
        ValidationException::class,
    ];

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  \Exception  $e
     * @return void
     */
    public function report(Exception $e) {
        return parent::report($e);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $e
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $e) {

        if (!config('app.debug') && !$this->isHttpException($e) && !$e instanceof HttpResponseException) {

                return redirect('404');
        }

        return parent::render($request, $e);
    }
}