Bug #88628

Redirect call on controller level is firing the action again

Added by Ronny Hauptvogel almost 3 years ago. Updated 4 months ago.

Status:
Rejected
Priority:
-- undefined --
Assignee:
-
Category:
Extbase
Target version:
-
Start date:
2019-06-25
Due date:
% Done:

0%

Estimated time:
TYPO3 Version:
9
PHP Version:
7.2
Tags:
Redirect
Complexity:
Is Regression:
Sprint Focus:

Description

I'm not really sure if this is a bug or something has changed here, but the behaviour in TYPO3 8 is not the same like in TYPO3 9.

It seems that a redirect call ($this->redirect()) on controller level will call the action again which have fired the redirect.
So if I do something like that:

class FormController extends ActionController
{
    public function showAction(): void
    {
        // nothing to do just show the form
    }

    public function redirectAction(): void
    {
        $mailData = [
            'sender' => $this->settings['senderEmail'],
            'senderName' => $this->settings['senderName'],
            'subject' => $this->settings['subject'],
            'receivers' => explode(',', $this->settings['receiverEmails']),
            'html' => '<h1>Redirect Test</h1>',
        ];

        if (MailerUtility::send($mailData)) {
            $this->redirect(
                null,
                null,
                null,
                null,
                $this->settings['redirectPageId']
            );
        }
    }
}

then the mail will be sent only once in TYPO3 8, but twice in TYPO3 9. Without the redirect call, the mail will be sent once in TYPO3 9, too. Maybe someone can tell me more about this behaviour?

Furthermore I have created a little example extension to reproduce this, which you can find in the attachment. The dependencies are set to TYPO3 9, but this example extension works in TYPO3 8, too.

  1. just install and enable the extension
  2. include the extension plugin and set some flexform data
  3. take a look at the frontend rendering of this plugin and submit the example form

Files

redirect-test.zip (10 KB) redirect-test.zip Ronny Hauptvogel, 2019-06-25 16:14
mail-example.png (57.2 KB) mail-example.png Ronny Hauptvogel, 2020-01-16 11:44
example.png (15.6 KB) example.png Ronny Hauptvogel, 2020-01-16 11:44

Related issues

Related to TYPO3 Core - Bug #91156: Double Action execution if redirect at its endNew2020-04-21

Actions
#1

Updated by Philipp Faber almost 3 years ago

Exactly having the same problem here. I had to add a manual call like this to make it work:

header ("Location: http://". $_SERVER['HTTP_HOST']."/products");

It's a bad hack though. This might have to do sth with the PHP version?

#2

Updated by Ronny Hauptvogel almost 3 years ago

@Philipp

I don't think that have something to do with the PHP version, because I have used the same PHP version for both (TYPO3 8 and TYPO3 9, PHP 7.2.X).

#3

Updated by Alexander Schnitzler over 2 years ago

  • Status changed from New to Needs Feedback

I can't reproduce this.
Can you tell me the exact scenario that causes this? I have problems understanding which plugin is on which page and from where you redirect where to.
I'd like to isolate the issues as much as possible.

#4

Updated by Ronny Hauptvogel over 2 years ago

@Alexander have you tried the attached extension above with TYPO3 9? And if so, did you get one or two mails?

The ticket is a little bit older and I don't know if this behaviour was fixed in the meantime, but I can recheck this.

There was no special scenario:

  1. Place the extension plugin on a page and set it up
  2. Open up the page in the frontend
  3. Submit the form

After that you should be redirected to the page which you have set up in the extension plugin. In my case this was always a another page without any plugin.
Furthermore you should get exactly one mail, but in my case I got always two mails, because the action was called twice if I have used the redirect call.

As I said I will recheck this with the current TYPO3 9 version.

#5

Updated by Ronny Hauptvogel over 2 years ago

@Alexander I have tested the extension against TYPO3 9.5.13 and this behaviour is sadly still present.

So I got two mails instead of one, because the redirectAction will be called twice.

#6

Updated by Riccardo De Contardi over 2 years ago

  • Status changed from Needs Feedback to New
#7

Updated by Alexander Schnitzler about 2 years ago

  • Status changed from New to Rejected
  • Priority changed from Should have to -- undefined --

So, I digged into this issue and tried the attached extension.

First thing I found is that the action which sends the email is marked as cacheable. In fact all actions in the plugin are cacheable which might not be what you want. As this action either redirects or displays an error I assume, it should not be cached but be dependent on the actual execution.

So much for the theory. It's important to understand why things happen the way they do.

When calling redirect in an action, Extbase (rightfully) assumes that this action shouldn't be cacheable as you don't want to cache the Redirect HTML and just return that when accessing the action without calling it's actual logic.
So, Extbase detects that the action is cachable and therefore converts the USER content object to a USER_INT and redispatches the plugin. That then triggers the second call of the plugin.

Now that we understand the cause, we can think about a solution. The easiest of course is to make your action uncached. That's the correct configuration anyway.

To solve this in Extbase is a bit tricky because we have some kind of race condition here. Extbase only knows that a cached action triggers a redirect as soon as the redirect is triggered. This means, Extbase cannot detect the need to convert the cached plugin to a non-cached one before execution. I am open for ideas but I think that this is something which isn't easily fixable. At least not until Extbase replaces it's inner handling of requests and responses based on a StopActionException instead of a response which then indicates an initiated redirect.

This is something I am working on since TYPO3 10 but the new request/response handling doesn't exist yet. That said, I fear the only solution is the first one, defining actions properly as non-cachable.

I will reject this method for now.
Feel free to add a comment.

#8

Updated by Ronny Hauptvogel about 2 years ago

Ok, thank you for your explanation and your time :).

It's totally fine for me to mark actions with a redirect call as uncachable and now I know what is happening in the background :).

#9

Updated by Michael Stopp about 2 years ago

  • Related to Bug #91156: Double Action execution if redirect at its end added
#10

Updated by Bernhard Eckl 4 months ago

I want to do the following:

if ($GLOBALS['TSFE']->fe_user->user['uid']) {
$this->addFlashMessage('No permission.', '', \TYPO3\CMS\Core\Messaging\AbstractMessage::WARNING);
$this->redirect('list');
}

But just because of the double flash message I have to define the new action as uncacheable. Acutally I removed the redirect and make a condition in the new template not showing the form if not logged in. Is there a better solution I can implement?

Also available in: Atom PDF