如何使用错误处理配置 return 来自 Extbase 的正确 404 错误

How to return a proper 404 error from Extbase with a error handling configuration

TYPO3 版本:9.5.4

目标: 我想 return 来自具有正确状态代码等的 Extbase 控制器的 404 错误,该控制器使用我设置的 404 错误处理配置在站点配置中。

检查 404 处理是否有效: 我在站点配置中设置了 404 错误处理。这应该显示特定页面的内容。如果我转到 www.my-domain.local/asdfasdf 我将获得一个 404 状态代码,其中包含我指定的页面内容

我在 Extbase 中尝试了什么:

# In the action    
return GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
        $GLOBALS['TYPO3_REQUEST'],
        'The requested page does not exist',
        ['code' => PageAccessFailureReasons::PAGE_NOT_FOUND]
    );

结果:

变体 A(站点配置中无错误处理):状态代码为 404 的异常 > O.K

变体 B(显示页面内容的错误处理):页面以状态 200 呈现,显示所述页面的正常内容(页眉、页脚等)> 不是 O.K

问题:如何让 Extbase 做与普通页面相同的事情?

如果您只是在操作中 return 此响应,则页面呈现正在进行中,不会被中断。将响应传递给 ImmediateResponseException 让 ErrorHandler 处理它。

$response = GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
    $GLOBALS['TYPO3_REQUEST'],
    'Your error message',
    ['code' => PageAccessFailureReasons::PAGE_NOT_FOUND]
);
throw new ImmediateResponseException($response);