Last active
May 10, 2023 21:46
-
-
Save einpraegsam/7ae7c5f225a96e340d3dc6d54a50fca0 to your computer and use it in GitHub Desktop.
Individual 403 and 404 handling in TYPO3 9 and 10. Show a 404 page but keep wrong URL. In case of a missing frontend login, redirect to login page and redirect back after login.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
base: 'https://%env(HOST)%/' | |
baseVariants: { } | |
errorHandling: | |
- | |
errorCode: '403' | |
errorHandler: PHP | |
errorPhpClassFQCN: Vendor\Sitepackage\PageHandler\NotAuthorized | |
- | |
errorCode: '404' | |
errorHandler: PHP | |
errorPhpClassFQCN: Vendor\Sitepackage\PageHandler\NotFound |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing! Not authorized works like a charm. Not found is not needed anymore in TYPO3 10 and later (URL is not changed when using Page Handler).