Anonymous user can't see the message(s) set before the redirect

Created on 16 July 2018, over 6 years ago
Updated 24 April 2023, over 1 year ago

I was using drupal_set_message and now MessengerInterface::addMessage to display status and warning messages from my custom modules.

I noticed anonymous users did not see these messages.

When removing my PHP header redirects, the messages do show up.
The strange part is that it does work for users that are logged in even when setting a PHP header redirect.

Is this intended?
What is the proper way to do this?

class MyCustomController extends ControllerBase {
  $this->messenger()->addMessage('This is a test.');
  header('Location: ' . '/');
  die();
}
💬 Support request
Status

Fixed

Version

8.8 ⚰️

Component
Base 

Last updated about 4 hours ago

Created by

🇧🇪Belgium Pascal-

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇮🇳India kmani

    Use messenger interface to display messages while redirecting

    use Drupal\Core\Messenger\MessengerInterface;

    $this->messenger->addMessage('This is a warning message (orange)', TRUE);

Production build 0.71.5 2024