Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 301
Florian Rachor Avatar asked Jan 27 '23 15:01

Florian Rachor


1 Answers

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);
like image 145
Mikel Wohlschlegel Avatar answered Mar 18 '23 00:03

Mikel Wohlschlegel