[PHP] Modern example for 403 and 404 handling in TYPO3

[PHP] Modern example for 403 and 404 handling in TYPO3

If you need an individual 403 or 404 handling in TYPO3 9 or newer, you may find it here. So you might want to deliver a 404 status, but keep the wrong URL? Or do you need a redirect to a login page when trying to access a protected page in the frontend?

config.yaml:

base: 'https://%env(HOST)%/' baseVariants: { } errorHandling: - errorCode: '403' errorHandler: PHP errorPhpClassFQCN: Vendor\Sitepackage\PageHandler\NotAuthorized - errorCode: '404' errorHandler: PHP errorPhpClassFQCN: Vendor\Sitepackage\PageHandler\NotFound

NotAuthorized.php:

<?php declare(strict_types=1); namespace Vendor\Sitepackage\PageHandler; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use TYPO3\CMS\Core\Error\PageErrorHandler\PageErrorHandlerInterface; use TYPO3\CMS\Core\Http\RedirectResponse; use TYPO3\CMS\Core\Http\Uri; use TYPO3\CMS\Core\Site\Entity\Site; use TYPO3\CMS\Core\Site\Entity\SiteLanguage; use TYPO3\CMS\Core\Site\SiteFinder; use TYPO3\CMS\Core\Utility\GeneralUtility; /** * Class NotAuthorized * to show a login page (in fitting language) and redirect to page after login * @noinspection PhpUnused */ class NotAuthorized implements PageErrorHandlerInterface { /** * @var int */ protected $pageLoginUid = 167; /** * @var ServerRequestInterface */ protected $request = null; /** * @param ServerRequestInterface $request * @param string $message * @param array $reasons * @return ResponseInterface */ public function handlePageError( ServerRequestInterface $request, string $message, array $reasons = [] ): ResponseInterface { $this->request = $request; return new RedirectResponse($this->getLoginUrl(), 302); } /** * @return string */ protected function getLoginUrl(): string { /** @var Site $site */ $siteFinder = GeneralUtility::makeInstance(SiteFinder::class); $site = $siteFinder->getSiteByPageId($this->pageLoginUid); /** @var Uri $uri */ $uri = $site->getRouter()->generateUri( $this->pageLoginUid, [ '_language' => $this->getLanguageIdentifier(), 'redirect_url' => $this->request->getUri()->__toString() ] ); return $uri->__toString(); } /** * @return int */ protected function getLanguageIdentifier(): int { /** @var SiteLanguage $language */ $language = $this->request->getAttribute('language'); return $language->getLanguageId(); } }

NotFound.php:

<?php declare(strict_types=1); namespace Vendor\Sitepackage\PageHandler; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use TYPO3\CMS\Core\Error\PageErrorHandler\PageErrorHandlerInterface; use TYPO3\CMS\Core\Http\HtmlResponse; use TYPO3\CMS\Core\Http\RequestFactory; use TYPO3\CMS\Core\Http\Uri; use TYPO3\CMS\Core\Site\Entity\Site; use TYPO3\CMS\Core\Site\Entity\SiteLanguage; use TYPO3\CMS\Core\Utility\GeneralUtility; /** * Class NotFound * to show a 404 page (in fitting language) * @noinspection PhpUnused */ class NotFound implements PageErrorHandlerInterface { /** * @var int */ protected $pageNotFoundUid = 133525; /** * @var ServerRequestInterface */ protected $request = null; /** * @param ServerRequestInterface $request * @param string $message * @param array $reasons * @return ResponseInterface * @throws RequestFailedException */ public function handlePageError( ServerRequestInterface $request, string $message, array $reasons = [] ): ResponseInterface { $this->request = $request; return new HtmlResponse($this->getPageNotFoundContent(), 404); } /** * @return string * @throws RequestFailedException */ protected function getPageNotFoundContent(): string { $url = $this->getPageNotFoundUrl(); $requestFactory = GeneralUtility::makeInstance(RequestFactory::class); /** @var ResponseInterface $response */ $response = $requestFactory->request($url, 'GET'); if ($response->getStatusCode() === 200) { if (strpos($response->getHeaderLine('Content-Type'), 'text/html') === 0) { return $response->getBody()->getContents(); } } throw new \LogicException('Could not read content of ' . $url, 1594134183); } /** * @return string */ protected function getPageNotFoundUrl(): string { /** @var Site $site */ $site = $this->request->getAttribute('site'); /** @var Uri $uri */ $uri = $site->getRouter()->generateUri($this->pageNotFoundUid, ['_language' => $this->getLanguageIdentifier()]); return $uri->__toString(); } /** * @return int */ protected function getLanguageIdentifier(): int { /** @var SiteLanguage $language */ $language = $this->request->getAttribute('language'); return $language->getLanguageId(); } }

TYPO3: Finding unused files in fileadmin

Do you want to delete unused or orphaned files in fileadmin or another storage location? Unfortunately, there's no direct core functionality for this. But a small command in your site package can...

Go to news

TYPO3: Editors with individual user_upload folders

Perhaps you're familiar with this client requirement? Editors should be able to add videos using the "Add media by URL" button. But the files shouldn't be located in fileadmin/user_upload/, but rather...

Go to news

TYPO3: Finding pages in mixed mode

In TYPO3, Mixed Mode refers to translated pages that contain content only partially related to the corresponding content in the main language. This is indicated in the backend by an error message. But...

Go to news

Extbase Extensions: Think extensibility with data, site and language

Today, I have a small request for the TYPO3 extension authors out there: Make sure your extensions are extensible. This will also promote the distribution of the corresponding plugins.

Go to news

SQL: Show all tables sorted by size in descending order

Lately I've been using the SQL command more often to find out which tables in the TYPO3 database are the largest. I've published the snippet once.

Go to news

TYPO3 12 with CKEditor 5: Styles in a single selection

If you set a link in the RTE in TYPO3, you may have to choose between different link classes, for example to create buttons in the frontend. What's new in TYPO3 12 is that you can select not just one...

Go to news