Search code examples
flow-framework

typo3 flow: catch exception and forward back to originating action


In my typo3 flow app I want to stop execution after throwing an exception as flash-message. Therefore I wrote this:

public function updateAction(Mitglied $mitglied) {

   if ($xy == 'z') {
       try {
           throw new \TYPO3\Flow\Validation\Exception\InvalidValidationOptionsException('Fehler: In dieser Kombination nicht zulässig', 1);
       } catch (\TYPO3\Flow\Validation\Exception\InvalidValidationOptionsException $e) {
           $this->flashMessageContainer->addMessage(new \TYPO3\Flow\Error\Error($e->getMessage()));
       }
   }

   $this->mitgliedRepository->update($mitglied);
   $this->addFlashMessage('Mitglied erfolgreich geändert.');
   $this->redirect('index');
}

The message ist shown, as I wanted, as flash-message. But the execution of the function doesn't stop. Does anybody know, why and how to prevent? A redirect to the originating action would be desired for the case, that the if-condition is true.


Solution

  • I got it now with the following code:

    // get back to originating request - see https://git.typo3.org/Packages/TYPO3.Flow.git/blob/master:/Classes/TYPO3/Flow/Mvc/Controller/ActionController.php
    
    $referringRequest = $this->request->getReferringRequest();
    if ($referringRequest === NULL) {
          return;
    }
    $packageKey = $referringRequest->getControllerPackageKey();
    $subpackageKey = $referringRequest->getControllerSubpackageKey();
    if ($subpackageKey !== NULL) {
            $packageKey .= '\\' . $subpackageKey;
    }
    $argumentsForNextController = $referringRequest->getArguments();
    $argumentsForNextController['__submittedArguments'] = $this->request->getArguments();
    $argumentsForNextController['__submittedArgumentValidationResults'] = $this->arguments->getValidationResults();
    $this->forward($referringRequest->getControllerActionName(), $referringRequest->getControllerName(), $packageKey, $argumentsForNextController);
    

    In the end this is much easier:

    $this->errorAction()->forwardToReferringRequest();