最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Laravel 11 - how to call the old 'Handler.php' class from app.php - withExceptions - Stack Overflow

programmeradmin2浏览0评论

I have recently created a new project in Laravel 11 and can not get my custom error handling class to work. I have a class I use throughout all my Larvael projects (laravel 10). I want to use this class in Laravel 11 to process all my errors, but it seems the new version relies on the withExceptions method in app.php

->withExceptions(function (Exceptions $exceptions) {

Does anyone know how I can get the withExceptions class to still use this (or any) external class for handling


use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler {

}

I have recently created a new project in Laravel 11 and can not get my custom error handling class to work. I have a class I use throughout all my Larvael projects (laravel 10). I want to use this class in Laravel 11 to process all my errors, but it seems the new version relies on the withExceptions method in app.php

->withExceptions(function (Exceptions $exceptions) {

Does anyone know how I can get the withExceptions class to still use this (or any) external class for handling


use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler {

}
Share Improve this question asked Mar 17 at 18:58 Jon MenardJon Menard 3862 silver badges14 bronze badges 1
  • I don't use Laravel, but have you seen the discussion here: github/laravel/framework/discussions/50902 – Chris Haas Commented Mar 17 at 19:05
Add a comment  | 

1 Answer 1

Reset to default 2

In Laravel 11, exception handling with custom classes registration has changed. To use your custom handler:

  1. Create your custom handler:
namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;

class CustomHandler extends ExceptionHandler
{
    public function render($request, Throwable $e)
    {
        // Custom logic
        return response()->json(['error' => $e->getMessage()], 500);
    }
}
  1. Register it in bootstrap/app.php:
$app->singleton( Illuminate\Contracts\Debug\ExceptionHandler::class, App\Exceptions\CustomHandler::class );

You can inject external classes into CustomHandler for additional processing.


More details:

  • Laravel 11 - Application Configuration

  • Laravel Exception Handling


发布评论

评论列表(0)

  1. 暂无评论