TYPO3 v10: Extbase: Send 404 page not found response

raw

README.rst

Using the global variable will stop working some time in the future:

The request object is also available as a global variable in $GLOBALS['TYPO3_REQUEST']. This is a workaround for the core which has to access the server parameters at places where $request is not available. So, while this object is globally available during any HTTP request, it is considered bad practice to use this global object if the request is accessible in another, official way. The global object is scheduled to vanish at a later point once the code has been refactored enough to not rely on it anymore.

From: https://docs.typo3.org/m/typo3/reference-coreapi/10.4/en-us/ApiOverview/RequestHandling/Typo3Request.html

raw

code.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
use TYPO3\CMS\Core\Http\ImmediateResponseException;   
use TYPO3\CMS\Core\Utility\GeneralUtility;            
use TYPO3\CMS\Frontend\Controller\ErrorController;    
 
class SomeController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
{
    public function someAction($id)
    {
        if (!SomeModel::find($id)) {
            $response = GeneralUtility::makeInstance(ErrorController::class)
                ->pageNotFoundAction($GLOBALS['TYPO3_REQUEST'], 'Record not found.');
            throw new ImmediateResponseException($response, 1617116194);    
        }
    }
}
Christian Weiske Christian Weiske
owner

History