How to redirect to 404 page in a extbase controller in typo3 V10

Viewed 1288

In TYPO3 v10 you can't use the $TSFE->pageNotFoundAndExit() anymore. But the $this->request in the controller action gives an exception when using the ErrorController PageNotFound method.

1 Answers
$TSFE->pageNotFoundAndExit() will be removed in TYPO3 v10.0. Use TYPO3's ErrorController with Request/Response objects instead.

(https://docs.typo3.org/c/typo3/cms-core/master/en-us/Changelog/9.2/Deprecation-83883-PageNotFoundAndErrorHandlingInFrontend.html)

In your controller you have to use the $GLOBALS['TYPO3_REQUEST'] instead of $this->request.

Tip: by using the ImmediateResponseException further action won't be invoked.

Example method:

   $response = GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
            $GLOBALS['TYPO3_REQUEST'],
            'your 404 message'
        );
        throw new ImmediateResponseException($response, 1591428020);
Related