如何在laravel 5中注册自定义错误处理程序? [英] How can I register custom error handler in laravel 5?

查看:94
本文介绍了如何在laravel 5中注册自定义错误处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm developing a Laravel package, have a service provider with views and everything, but I need to have custom error messages. How can I register custom error handler in my service provider?

推荐答案

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

You can register custom handler by binding it with Laravel's exception handler class on service provider.

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

First you have to create custom exception handler class.

<?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/

For more information check this blog http://blog.sarav.co/registering-custom-exception-handler-laravel-5/

这篇关于如何在laravel 5中注册自定义错误处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆