Kroff
Kroff

Reputation: 61

TYPO3 9.5 Extbase How redirect a call of showAction with a invalide object to a custom page (not 404!)

I want to redirect a call of a showAction with an invalid uid to a custom page. How can I do this with TYPO3 9 when routing and a 404 handling is active? At moment I always end at the 404 because the page is not found..

Where should I attack?

checking plugin settings like throwPageNotFoundExceptionIfActionCantBeResolved? ignore param validation? overwrite errorAction or callActionMethod? write a custom global 404 handling? overwrite routing?

routing + error handling config:

...
    type: Extbase
    extension: ext
    plugin: Pi1
    routes:
      -
        routePath: '/{object}'
        _controller: 'object::show'
        _arguments:
          object: object
    defaultController: 'object::list'
    defaults:
      page: '0'
    requirements:
      object: '^[0-9].*$'
      page: \d+
    aspects:
      object:
        type: PersistedAliasMapper
        tableName: tx_ext_domain_model_object
        routeFieldName: uid
      page:
        type: StaticRangeMapper
        start: '1'
        end: '100'
...

errorHandling:
  -
    errorCode: '404'
    errorHandler: Page
    errorContentSource: 't3://page?uid=174'

Upvotes: 1

Views: 1063

Answers (2)

Philip
Philip

Reputation: 1

First of all, custom error handling is an easy way to solve the problem. Given solution by Kroff works fine.

But, it is just working in case of pages with one language. And it is not really useful to add a static path in the comparison (due to possibly changing page slugs).

Has anyone found a solution to check the called page id? Or even better add a default route directly in the route enhancers configuration?

By the way, if you want to keep the typed in url and still show the 404 page (default of typo3 page error handler) just change the return value return parent::handlePageError($request, $message, $reasons); and extend class by PageContentErrorHandler.

Upvotes: 0

Kroff
Kroff

Reputation: 61

Ok, the best way is to overwrite the TYPO3-Errorhandling..

config.yaml

errorHandling:
-
  errorCode: '404'
  errorHandler: PHP
  errorPhpClassFQCN: My\secret\namespace\Error\ErrorHandling

ErrorHandling.php

namespace My\secret\namespace\Error;


use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Error\PageErrorHandler\PageErrorHandlerInterface;
use TYPO3\CMS\Core\Http\RedirectResponse;

class ErrorHandling implements PageErrorHandlerInterface{

/**
 * @param ServerRequestInterface $request
 * @param string $message
 * @param array $reasons
 * @return ResponseInterface
 */
public function handlePageError(ServerRequestInterface $request, string $message, array $reasons = []): ResponseInterface{

    if (strpos($request->getRequestTarget(), '/page-where-i-want-my-special-404') !== false) {
        return new RedirectResponse('/my-custom-404', 404);
    }

    return new RedirectResponse('/404', 404);
    }
}

Upvotes: 1

Related Questions