TYPO3 Extbase - redirect to pid
Asked Answered
K

5

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('');

}
Keil answered 11/11, 2016 at 15:44 Comment(0)
C
18

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);
Chic answered 11/11, 2016 at 16:8 Comment(1)
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 $uriBuilderTopknot
E
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);
Elspeth answered 11/11, 2016 at 16:12 Comment(0)
K
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);
Keil answered 14/11, 2016 at 7:25 Comment(0)
M
0

I use it like this:

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

note that I use the URL that realURL generates.

Mccandless answered 11/11, 2016 at 15:59 Comment(2)
I would like to redirect on a completely different page by PID. For example, on the homepage (ID 1)Keil
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/Mccandless
B
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
Bootee answered 11/7, 2020 at 1:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.