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?

$redirect = new \Symfony\Component\HttpFoundation\RedirectResponse($login->toString());
$request = \Drupal::request();
$session = $request->getSession();
$session->set('msg', $config->get('girisyapmesaji'));
$redirect = new RedirectResponse($login->toString());
  • You can't send a response from custom code. Either return the response from a controller or set it in an event.
    – 4uk4
    Dec 30, 2020 at 16:13
  • That's not an option, as I need to use it in a hook_entity_view implementation.
    – Kartagis
    Dec 31, 2020 at 14:18
  • This is not possible. If you want to react on an entity displayed as main content you could check the route in a request event subscriber.
    – 4uk4
    Dec 31, 2020 at 14:44
  • What is not possible? Showing a message to the anonymous user in hook_entity_view or something else?
    – Kartagis
    Jan 1, 2021 at 15:24
  • Sending the redirect response.
    – 4uk4
    Jan 1, 2021 at 15:47

1 Answer 1


In general, never send responses from custom code. See How do I set a cookie using $response->headers->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:



namespace Drupal\mymodule\EventSubscriber;

use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\RequestEvent;
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->routeMatch = $route_match;

   * Kernel request event handler.
   * @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
   *   The request event.
  public function onKernelRequest(RequestEvent $event) {
    if (!$event->isMainRequest()) {
    if (!$this->routeMatch->getRouteName() == 'entity.node.canonical') {
    $request = $event->getRequest();
    if ($this->routeMatch->getRawParameter('node') == '1') {
      if ($some_condition) {
        \Drupal::messenger()->addStatus('My message');
        $redirect = new RedirectResponse('/foo/bar', 302);

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



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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.