Philipp M
Philipp M

Reputation: 3498

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

  }

}

Upvotes: 2

Views: 9577

Answers (3)

René Pflamm
René Pflamm

Reputation: 3354

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.

Upvotes: 5

Markus Kappe
Markus Kappe

Reputation: 185

This should do the trick:

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

Alternative Solution (from Simon Oberländer)

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

Your question suggests that there should be an other action without arguments, probably a listAction, that is the DEFAULT action. The default action gets called when no action is specified. It is the first action enlisted in the ExtensionUtility::configurePlugin() call.

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

Regarding > The identity property "TTTT" is no UID

You have to distinguish between no parameter and an invalid parameter. For the latter you can add @ignorevalidation to the showAction comments and do your validation testing within the action - or you can leave it to extbase that displays the error message you have seen. Where would you get a link like domain.com/abc/TTTT/ from anyhow? Unless the link is expired.

BTW: in a production system you would disable the display of exceptions, thus the display of the website would work.

Upvotes: 1

coding.ms
coding.ms

Reputation: 91

This could be a solution:

``` /** * Show a booking object * * @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);
}

```

Upvotes: -1

Related Questions