Search code examples
typo3extbasetypo3-8.xtypo3-extensions

TYPO3 Extension - Redirect to another page in show action if no record is set or not available


How can I redirect to another page when someone access the detail page but without a record or if record is not available?

I have detail records like

domain.com/abc/ABC1234

When somone enters

domain.com/abc/

... I get:

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. 

... in this case I want it to redirect to:

domain.com/other-page/

... I also need it if a specific record is not available.
... how to do so?

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

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

}

Here are some examples TYPO3 Extbase - redirect to pid ... but not sure how to implement it

Edit: What works is ...

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

  }

}

Solution

  • The redirect method needs an action and controller parameter. So your redirect code is wrong.

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

    To redirect to an PageUID you need to use the uriBuilder and the redirectToUri method. See here for an example.