How can I redirect user, while showing them a status message? I'm working on a custom module, in which I want to redirect users, while showing them a status message. What I've tried so far only works for authenticated users but I want to redirect anonymous users. I've tried the below solutions so far. What else can I do?
\Drupal::messenger()-&gt;addStatus($config-&gt;get('mymessage');
$redirect = new \Symfony\Component\HttpFoundation\RedirectResponse($login-&gt;toString());
$redirect-&gt;send();

$request = \Drupal::request();
$session = $request-&gt;getSession();
$session-&gt;set('msg', $config-&gt;get('girisyapmesaji'));
$session-&gt;save();
\Drupal::messenger()-&gt;addStatus($session-&gt;get('msg'));
$redirect = new RedirectResponse($login-&gt;toString());
$redirect-&gt;send();
In general, never send responses from custom code. See How do I set a cookie using $response-&gt;headers-&gt;setCookie() in a form submission handler?
In this case you are sending a redirect response to the client while Drupal builds the real response containing the cookie to the session where the message gets stored. The real response never reaches the client because you've already sent them away.
If you don't have a controller to return the redirect you can use an event subscriber to set the response the Drupal kernel returns. See this code example https://drupal.stackexchange.com/a/274494/47547.
In your case you would set a status message instead of a custom cookie:
/src/EventSubscriber/MymoduleSubscriber.php
&lt;?php

namespace Drupal\mymodule\EventSubscriber;

use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpFoundation\RedirectResponse;

/**
 * mymodule event subscriber.
 */
class MymoduleSubscriber implements EventSubscriberInterface {

  /**
   * The route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * Constructs event subscriber.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match.
   */
  public function __construct(RouteMatchInterface $route_match) {
    $this-&gt;routeMatch = $route_match;
  }

  /**
   * Kernel request event handler.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   Response event.
   */
  public function onKernelRequest(GetResponseEvent $event) {
    if (!$event-&gt;isMasterRequest()) {
      return;
    }
    if (!$this-&gt;routeMatch-&gt;getRouteName() == 'entity.node.canonical') {
      return;
    }
    $request = $event-&gt;getRequest();
    if ($this-&gt;routeMatch-&gt;getRawParameter('node') == '1') {
      \Drupal::service('page_cache_kill_switch')-&gt;trigger();
      if ($some_condition) {
        \Drupal::messenger()-&gt;addStatus('My message');
        $redirect = new RedirectResponse('/foo/bar', 302);
        $event-&gt;setResponse($redirect);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      KernelEvents::REQUEST =&gt; ['onKernelRequest', 31],
    ];
  }

}
   

mymodule.services.yml
services:
  mymodule.event_subscriber:
    class: Drupal\mymodule\EventSubscriber\MymoduleSubscriber
    arguments: ['@current_route_match']
    tags:
      - { name: event_subscriber }