Laravel 5.8 到 6.x 升级错误 - App\Exceptions\Handler::renderHttpException 声明

Posted

技术标签:

【中文标题】Laravel 5.8 到 6.x 升级错误 - App\\Exceptions\\Handler::renderHttpException 声明【英文标题】:Laravel 5.8 to 6.x upgrade error - Declaration of App\Exceptions\Handler::renderHttpExceptionLaravel 5.8 到 6.x 升级错误 - App\Exceptions\Handler::renderHttpException 声明 【发布时间】:2020-10-10 08:00:04 【问题描述】:

我最近将项目中的 Laravel 从 5.8.* 升级到了 6.x 版本。

现在,当我尝试运行命令 composer dump-autoload -o 时,我收到此错误:

Declaration of App\Exceptions\Handler::renderHttpException(Symfony\Component\HttpKernel\Exception\HttpException $e)
   should be compatible with Illuminate\Foundation\Exceptions\Handler::renderHttpException(Symfony\Component\HttpKern
  el\Exception\HttpExceptionInterface $e)

下面是项目中设置的composer.json,当我将 Laravel 从 5.7.* 升级到 5.8.* 时,我也遇到了与上述相同的错误,但我进一步升级希望能解决问题:


    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "repositories": [
        
        "type": "composer",
        "url": "https://packages.[company_api].com"
        
    ],
    "require": 
        "php": ">=7.1.3",
        "basemkhirat/elasticsearch": "^1.3",
        "botman/botman": "^2.5",
        "botman/driver-web": "^1.5",
        "deployer/deployer": "^6.0",
        "fideloper/proxy": "^4.0",
        "guzzlehttp/guzzle": "^6.3",
        "jenssegers/agent": "^2.6",
        "jimmiw/php-time-ago": "^2.0",
        "laravel/framework": "^6.0",
        "laravel/scout": "^8.0",
        "laravel/tinker": "~1.0",
        "[company_name]/wordpressblogapi": "1.4.*"
    ,
    "require-dev": 
        "filp/whoops": "~2.0",
        "fzaninotto/faker": "~1.4",
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "~7.0"
    ,
    "autoload": 
        "classmap": [
            "database/seeds",
            "database/factories"
        ],
        "psr-4": 
            "App\\": "app/"
        ,
      "files": [
        "helpers/helpers.php"
      ]
    ,
    "autoload-dev": 
        "psr-4": 
            "Tests\\": "tests/"
        
    ,

    "scripts": 
        "post-root-package-install": [
            "php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate"
        ],
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover"
        ]
    ,
    "config": 
        "preferred-install": "dist",
        "sort-packages": true,
        "optimize-autoloader": true
    

我已经参考了文档,但没有具体说明任何 ExceptionHandler.php 文件更改,所以任何人都建议如何解决这个问题...

【问题讨论】:

【参考方案1】:

这是一个 5.7 到 5.8 的升级问题,正如您在问题开始时指出的那样。该方法的签名已更改:

Illuminate\Foundation\Exceptions\Handler 类的 renderHttpException 方法签名已更改。如果您在异常处理程序中重写此方法,则应更新方法签名以匹配其父级:”

/**
 * Render the given HttpException.
 *
 * @param  \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface  $e
 * @return \Symfony\Component\HttpFoundation\Response
 */
protected function renderHttpException(HttpExceptionInterface $e);

Laravel 5.8 Docs - Upgrade Guide - Exception Handling

【讨论】:

以上是关于Laravel 5.8 到 6.x 升级错误 - App\Exceptions\Handler::renderHttpException 声明的主要内容,如果未能解决你的问题,请参考以下文章

未捕获的错误:将 laravel 5.8 升级到 8 后调用未定义的函数 Illuminate\Mail\TransportManager()

从 laravel 6.x 升级到 laravel 7.0 时出错

从 Laravel 5.1 升级到 Laravel 5.8 后 whereHas() 变慢

从 Laravel 5.8 升级到 6.2 后 ConfirmPasswordController 不存在

Laravel 5.8 验证错误返回到所有输入的特定视图-> 旧的不起作用

Laravel 5.8:一般错误:1005 无法创建表