TYPO3 Version: 9.5.4
Goal: I want to return a 404 error from an Extbase controller with the proper status code etc. that uses the configuration for 404 error handling I set up in the site configuration.
Check that 404 handling works: I setup a 404 error handling in the site configuration. This should show the content of a specific page. If I go to www.my-domain.local/asdfasdf I will get a 404 status code with the content of the page I specified
What did I try in Extbase:
# In the action
return GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
$GLOBALS['TYPO3_REQUEST'],
'The requested page does not exist',
['code' => PageAccessFailureReasons::PAGE_NOT_FOUND]
);
Result:
Variant A (No error handling in site configuration): Exception with status code 404 > O.K
Variant B (Error handling to show content of page): Page renders with status 200, shows normal content of said page (header, footer etc.) > Not O.K
Question: How do I get Extbase to do the same thing as the normal pages?
If you just return this response in your action, the page rendering is in process and does not get interrupted. Pass the response to an ImmediateResponseException to let the ErrorHandler handle it.
$response = GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
$GLOBALS['TYPO3_REQUEST'],
'Your error message',
['code' => PageAccessFailureReasons::PAGE_NOT_FOUND]
);
throw new ImmediateResponseException($response);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With