简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM