TYPO3 Extension - 如果没有设置记录或不可用,则在show动作中重定向到另一个页面

时间:2018-03-21 10:15:16

标签: typo3 extbase typo3-8.x typo3-extensions

当有人访问详细信息页面但没有记录或记录不可用时,如何重定向到另一个页面?

我有详细记录,如

domain.com/abc/ABC1234

当somone进入时

domain.com/abc/

......我明白了:

Uncaught TYPO3 Exception
#1298012500: Required argument "record" is not set for Vendor\Extension\Controller\ActionController->show. (More information)

TYPO3\CMS\Extbase\Mvc\Controller\Exception\RequiredArgumentMissingException thrown in file
/is/htdocs/www/typo3_src-8.7.11/typo3/sysext/extbase/Classes/Mvc/Controller/AbstractController.php in line 425. 

...在这种情况下,我希望它重定向到:

domain.com/other-page/

......如果没有特定记录,我也需要它 ......怎么做?

/**
 * action show
 *
 * @param \Action $record
 * @return void
 */
public function showAction(Action $record) {

    $this->view->assign('record', $record);

}

以下是一些示例TYPO3 Extbase - redirect to pid ...但不确定如何实施

编辑:有效的是......

/**
 * action show
 *
 * @param \Action $record
 * @return void
 */
public function showAction(Action $record=null) {   

  if ($record === null) { 
    $pageUid = 75;
    $uriBuilder = $this->uriBuilder;
    $uri = $uriBuilder
      ->setTargetPageUid($pageUid)
      ->build();
    $this->redirectToUri($uri, 0, 404);
  } else {
    $this->view->assign('record', $record);

  }

}

3 个答案:

答案 0 :(得分:2)

redirect方法需要一个动作和控制器参数。所以你的重定向代码是错误的。

$this->redirect($actionName, $controllerName = NULL, $extensionName = NULL, array $arguments = NULL, $pageUid = NULL, $delay = 0, $statusCode = 303); 

要重定向到PageUID,您需要使用uriBuilder和redirectToUri方法。有关示例,请参阅here

答案 1 :(得分:1)

这应该可以解决问题:

public function showAction(Action $record=null) {
  if ($record === null) { 
    $this->redirect(/* add parameters as needed */); 
  } else {
    // other code
  }

替代解决方案(来自SimonOberländer)

public function intializeShowAction() {
  if (!$this->request->hasArgument('record')) {
    $this->redirect(/* add parameters as needed */); // stops further execution
  }
}

您的问题表明应该有一个没有参数的其他操作,可能是listAction,即DEFAULT操作。未指定操作时将调用默认操作。这是在ExtensionUtility :: configurePlugin()调用中登记的第一个动作。

\TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin(
    'Vendor.' . $_EXTKEY,
    'Pluginname',
    array(
        'Domainobject' => 'list, show',
    ),
    // non-cacheable actions
    array(
        'Domainobject' => 'list, show',
    )
);

关于>身份属性“TTTT”不是UID

您必须区分无参数和无效参数。对于后者,您可以将@ignorevalidation添加到showAction注释中并在操作中进行验证测试 - 或者您可以将其留给显示您看到的错误消息的extbase。 无论如何,你会从哪里获得domain.com/abc/TTTT/这样的链接?除非链接已过期。

BTW:在生产系统中,您将禁用异常显示,因此网站的显示将起作用。

答案 2 :(得分:-1)

这可能是一个解决方案:

``` / **  *显示预订对象  *  * @return void  * @throws \ TYPO3 \ CMS \ Extbase \ Mvc \ Exception \ NoSuchArgumentException  * /

public function showAction()
{
    $bookingObject = null;
    $bookingObjectUid = 0;
    if ($this->request->hasArgument('bookingObject')) {
        $bookingObjectUid = (int)$this->request->getArgument('bookingObject');
    }
    if ($bookingObjectUid > 0) {
        $bookingObject = $this->bookingObjectRepository->findByIdentifier($bookingObjectUid);
    }
    if (!($bookingObject instanceof BookingObject)) {
        $messageBody = 'Booking object can\'t be displayed.';
        $messageTitle = 'Error';
        $this->addFlashMessage($messageBody, $messageTitle, AbstractMessage::ERROR);
        $this->redirect('list');
    }
    $this->view->assign('bookingObject', $bookingObject);
}

```