如何在 laravel 5 中注册自定义错误处理程序?

2024-05-21

我正在开发一个 Laravel 包,有一个提供视图和一切的服务提供商,但我需要自定义错误消息。如何在我的服务提供商中注册自定义错误处理程序?


您可以通过将自定义处理程序与服务提供者上的 Laravel 异常处理程序类绑定来注册自定义处理程序。

创建自定义处理程序

首先,您必须创建自定义异常处理程序类。

<?php
namespace App\Exceptions;

use Exception;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class CustomHandler extends ExceptionHandler
{
    /**
     * A list of the exception types that should not be reported.
     *
     * @var array
     */
    protected $dontReport = [
        \Illuminate\Auth\AuthenticationException::class,
        \Illuminate\Auth\Access\AuthorizationException::class,
        \Symfony\Component\HttpKernel\Exception\HttpException::class,
        \Illuminate\Database\Eloquent\ModelNotFoundException::class,
        \Illuminate\Session\TokenMismatchException::class,
        \Illuminate\Validation\ValidationException::class,
    ];

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

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        if ($exception instanceof NotFoundHttpException) {
            return response()->view('errors.404', [], 404);
        }
        return parent::render($request, $exception);
    }

    /**
     * Convert an authentication exception into an unauthenticated response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Illuminate\Auth\AuthenticationException  $exception
     * @return \Illuminate\Http\Response
     */
    protected function unauthenticated($request, AuthenticationException $exception)
    {
        if ($request->expectsJson()) {
            return response()->json(['error' => 'Unauthenticated.'], 401);
        }
        return redirect()->guest('login');
    }
}

注册您的处理程序

现在在您的网站上注册该课程AppServiceProvider

<?php
namespace App\Providers;

use App\Exceptions\CustomHandler;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        /**
         * Do not forget to import them before using!
         */ 
        $this->app->bind(
            ExceptionHandler::class,
            CustomHandler::class
        );   
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {

    }
}

欲了解更多信息,请查看此博客http://blog.sarav.co/registering-custom-exception-handler-laravel-5/ http://blog.sarav.co/registering-custom-exception-handler-laravel-5/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 laravel 5 中注册自定义错误处理程序? 的相关文章

随机推荐