For coders TYPO3 Tech Corner

[PHP] Avoid TYPO3 log entries like "The page is not configured! [type=...][]"

[PHP] Avoid TYPO3 log entries like "The page is not configured! [type=...][]"

Do you know that too? You haven't had a TypeNum 98, 99, 100 or 3135 in your website configuration for a long time and yet there are still such requests on your site. These then lead to an error message and a status of 500. If you do not want to block all of these types in the .htaccess, you can also hook early into the system via PSR-15 middleware and check if the requested type fits to the site configuration. In the example below, we then simply redirect such requests to the start page with the header code 301.

EXT:sitepackage/Configuration/RequestMiddlewares.php:

<?php return [ 'frontend' => [ 'sitepackage-undefinedtypenumerrorhandling' => [ 'target' => \Vendor\Sitepackage\Middleware\UndefinedTypeNumErrorHandling::class, 'before' => [ 'typo3/cms-redirects/redirecthandler' ], 'after' => [ 'typo3/cms-frontend/site', ] ] ] ];

EXT:sitepackage/Classes/Middleware/UndefinedTypeNumErrorHandling.php:

<?php declare(strict_types=1); namespace Vendor\Sitepackage\Middleware; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use TYPO3\CMS\Core\Http\RedirectResponse; use TYPO3\CMS\Core\Site\Entity\Site; /** * Class UndefinedTypeNumErrorHandling * to simply redirect to "/" if a typeNum is given, that is not defined in siteconfiguration. * This avoids annoying log entries and delivers a 301 to the (e.g.) bot that is calling the outdated url. */ class UndefinedTypeNumErrorHandling implements MiddlewareInterface { /** * @param ServerRequestInterface $request * @param RequestHandlerInterface $handler * @return ResponseInterface */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { if ($this->isTypeNumSet($request) && $this->isTypeNumAllowed($request) === false) { return new RedirectResponse('/', 301); } return $handler->handle($request); } /** * @param ServerRequestInterface $request * @return bool */ protected function isTypeNumSet(ServerRequestInterface $request): bool { if (array_key_exists('type', $request->getQueryParams())) { return $request->getQueryParams()['type'] > 0; } return false; } /** * @param ServerRequestInterface $request * @return bool */ protected function isTypeNumAllowed(ServerRequestInterface $request): bool { /** @var Site $site */ $site = $request->getAttribute('site'); if (!empty($site->getConfiguration()['routeEnhancers']['PageTypeSuffix']['map'])) { $allowedTypeNums = array_values($site->getConfiguration()['routeEnhancers']['PageTypeSuffix']['map']); return in_array($request->getQueryParams()['type'], $allowedTypeNums); } return false; } }

Note: If you use this or a similar snippet, you have to make sure that all of your TypeNum definitions have also been included in the site configuration - e.g .:

rootPageId: 1 routes: - route: robots.txt type: staticText content: "Disallow: /typo3/\r\n" routeEnhancers: PageTypeSuffix: type: PageType default: / index: '' suffix: / map: print.html: 99 preview.html: 1560777975 newsletter.html: 1562349004 pixel.png: 1561894816

Back

"Code faster, look at the time" - does this sound familiar to you?

How about time and respect for code quality? Working in a team? Automated tests?

Join us