6
$GLOBALS['TSFE']->pageNotFoundAndExit('');

is currently used, but instead I would like to redirect to a page ID.

With the command redirectToUri, I could find no solution, or didn't work.

Code:

/**
* initialize action show
* @return void
*/
public function initializeShowAction() {
  if ($this->request->hasArgument('xxx')) {
    if ( $xxx=$this->xxxRepository->findByUid(
      intval($this->request->getArgument('xxx'))
    ) ) {
      $this->request->setArgument('xxx',$xxx);
      return;
    }
  }

$GLOBALS['TSFE']->pageNotFoundAndExit('');

}
Daniel
  • 6,916
  • 2
  • 36
  • 47
Stigi
  • 109
  • 1
  • 1
  • 8

5 Answers5

17

You can build an uri with the following code in your controller:

$uriBuilder = $this->uriBuilder;
$uri = $uriBuilder
  ->setTargetPageUid($pageUid)
  ->build();
$this->redirectToUri($uri, 0, 404);
René Pflamm
  • 3,273
  • 17
  • 29
  • 2
    i think it is better to use direct $this->uriBuilder if you are in controller like: `$uri = $this->uriBuilder ->setTargetPageUid($pageUid) ->build();` i see no reason to initialize variable $uriBuilder – patryno Jan 11 '19 at 08:38
11

In your controller you can use one of the following:

# Internal redirect of request to another controller
$this->forward($actionName, $controllerName, $extensionName, array $arguments);

# External HTTP redirect to another controller
$this->redirect($actionName, $controllerName, $extensionName, array $arguments, $pageUid, $delay = 0, $statusCode = 303);

# Redirect to URI
$this->redirectToURI($uri, $delay=0, $statusCode=303);

# Send HTTP status code
$this->throwStatus($statusCode, $statusMessage, $content);
jokumer
  • 2,249
  • 11
  • 22
2

Thank you to everyone. My solution is now:

$pageUid = $this->settings['myflexformsettingpart'];
$uriBuilder = $this->uriBuilder;
$uri = $uriBuilder
  ->setTargetPageUid($pageUid)
  ->build();
$this->redirectToURI($uri, $delay=0, $statusCode=303);
Stigi
  • 109
  • 1
  • 1
  • 8
0

I use it like this:

$GLOBALS['TYPO3_CONF_VARS']['FE']['pageNotFound_handling'] = '/my/404/';

note that I use the URL that realURL generates.

nbar
  • 6,028
  • 2
  • 24
  • 65
  • I would like to redirect on a completely different page by PID. For example, on the homepage (ID 1) – Stigi Nov 11 '16 at 16:00
  • You can also use `$GLOBALS['TYPO3_CONF_VARS']['FE']['pageNotFound_handling'] = 'index.php?id=999';` where 999 is the PID. But I am not sure if realURL would change that URL. So I suggest to use the URL that realURL generates for your `completely different page`. For your homepage (assumed its called home) just use `/home/` instead of `/my/404/` – nbar Nov 11 '16 at 16:04
0

You could also use the redirect Function of your Controller. Like:

    if(!$pageUid = intval($this->settings['resultPageUid']))
    {
        $pageUid = $GLOBALS['TSFE']->id;
    }
    $this->redirect(null, null, null, null, $pageUid);

So if no page uid to redirect to is found in your settings or the found uid is no integer, it will be redirected to the current page uid.

The redirect function allows 2 more parameters:

  • delay, int, default 0
  • statusCode, int, default 303
Mogens
  • 548
  • 1
  • 3
  • 10