- Create src/EventSubscriber/RequestSubscriber.php in your module.
- Define the RequestSubscriber class, which implements the EventSubscriberInterface interface:
<?php
namespace Drupal\mymodule\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class RequestSubscriber implements EventSubscriberInterface {
}
- To satisfy the interface requirements, we must add a getSubscribedEvents method. This tells the system which events we are subscribing to and the method that needs to be invoked:
<?php
namespace Drupal\mymodule\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
class RequestSubscriber implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
KernelEvents::REQUEST => ['doAnonymousRedirect', 28],
];
}
}
The KernelEvents class provides constants for available events. Our returned array specifies the method to invoke and its priority for that event.
Priorities will be discussed in the How it works... section. It is provided in the example to resolve possible conflicts when the dynamic_page_cache module is enabled.
- Create the doAnonymousRedirect method we specified, which will receive a GetResponseEvent argument:
<?php
namespace Drupal\mymodule\EventSubscriber;
use Drupal\Core\Url;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class RequestSubscriber implements EventSubscriberInterface {
/**
* Redirects all anonymous users to the login page.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
* The event.
*/
public function doAnonymousRedirect(GetResponseEvent $event) {
// Make sure we are not on the user login route.
if (\Drupal::routeMatch()->getRouteName() == 'user.login') {
return;
}
// Check if the current user is logged in.
if (\Drupal::currentUser()->isAnonymous()) {
// If they are not logged in, create a redirect response.
$url = Url::fromRoute('user.login')->toString();
$redirect = new RedirectResponse($url);
// Set the redirect response on the event, cancelling default response.
$event->setResponse($redirect);
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
KernelEvents::REQUEST => ['doAnonymousRedirect', 28],
];
}
}
To prevent a redirect loop, we will use the RouteMatch service to get the current route object and verify that we are not already on the user.login route page.
Then we check whether the user is anonymous and, if the user is anonymous, set the event's response to a redirect response.
- Now that we have created our class, create a mymodule.services.yml file in your module's directory.
- We must register our class with the service container so that Drupal understands that it will act as an event subscriber.
services:
mymodule.request_subscriber:
class: Drupal\mymodule\EventSubscriber\RequestSubscriber
tags:
- { name: event_subscriber }
The event_subscriber tag tells the container to invoke the getSubscribedEvents method and register its methods.
- Install the module or rebuild Drupal's caches if it has been already installed.
- Navigate to any page as an anonymous user--you will be redirected to the login form.