TYPO3 Extbase - 跳转到 pid

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

目前使用的是重定向到URL,但我希望重定向到页面ID。

使用命令redirectToUri,我找不到解决方案或者它没有起作用。

代码:

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

}
5个回答

17

您可以在控制器中使用以下代码构建URI:

$uriBuilder = $this->uriBuilder;
$uri = $uriBuilder
  ->setTargetPageUid($pageUid)
  ->build();
$this->redirectToUri($uri, 0, 404);

2
如果您在控制器中,我认为最好使用直接的 $this->uriBuilder,例如:$uri = $this->uriBuilder ->setTargetPageUid($pageUid) ->build();我不认为初始化变量 $uriBuilder 有任何必要。 - patryno

11

在您的控制器中,您可以使用以下其中一个:

# 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);

2
谢谢大家。我的解决方案现在是:
$pageUid = $this->settings['myflexformsettingpart'];
$uriBuilder = $this->uriBuilder;
$uri = $uriBuilder
  ->setTargetPageUid($pageUid)
  ->build();
$this->redirectToURI($uri, $delay=0, $statusCode=303);

0
我是这样使用它的:
$GLOBALS['TYPO3_CONF_VARS']['FE']['pageNotFound_handling'] = '/my/404/';

请注意,我使用realURL生成的URL。

我想通过PID在完全不同的页面上进行重定向。 例如,在主页(ID 1)上。 - Stigi
你也可以使用 $GLOBALS['TYPO3_CONF_VARS']['FE']['pageNotFound_handling'] = 'index.php?id=999';,其中 999 是 PID。但我不确定 realURL 是否会更改该 URL。因此,我建议使用 realURL 为你的“完全不同的页面”生成的 URL。对于你的主页(假设它叫 home),只需使用 /home/ 而不是 /my/404/ - nbar

0

你还可以使用控制器的重定向功能。例如:

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

如果在您的设置中找不到要重定向的页面UID或找到的UID不是整数,则会将其重定向到当前页面UID。

重定向函数允许使用两个参数:

  • delay,int,默认值为0
  • statusCode,int,默认值为303

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接