TYPO3版本:9.5.4
目标:我想从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与普通页面做同样的事情?
如果您只是在操作中返回此响应,则页面呈现正在进行中并且不会被中断。将响应传递给ImmediateResponseException以让ErrorHandler处理它。
$response = GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
$GLOBALS['TYPO3_REQUEST'],
'Your error message',
['code' => PageAccessFailureReasons::PAGE_NOT_FOUND]
);
throw new ImmediateResponseException($response);