I'm looking for a way to get the hostname of a multisite, and I've found several answers about it.

One of the solutions was to use: Answer link

$host= \Drupal::request()->getHost();



Per also a user comments that the use of the Request object can be dangerous, as mentioned in the following link.

The comment warning say: Url to documentation

Note: The use of this wrapper in particular is especially discouraged. Most code should not need to access the request directly. Doing so means it will only function when handling an HTTP request, and will require special modification or wrapping when run from a command line tool, from certain queue processors, or from automated tests.

If code must access the request, it is considerably better to register an object with the Service Container and give it a setRequest() method that is configured to run when the service is created. That way, the correct request object can always be provided by the container and the service can still be unit tested.

If this method must be used, never save the request object that is returned. Doing so may lead to inconsistencies as the request object is volatile and may change at various times, such as during a subrequest.

I have a custom module and I have a service, is it possible to implement it in a service?

My question is according to the comment and recommendation of the API, how is it that it should be implemented?


  • 1
    The ViewExecutable class follows that pattern. The factory service (ViewExecutableFactory) instantiates a View object based on the current HTTP request (from the injected request_stack service), and passes it through to the View. I would go with that approach
    – Clive
    Commented Oct 17, 2018 at 15:19

1 Answer 1


Here's an example (extracted from real code I just wrote in for Drupal 10.1) of getting the current request from the request_stack service in a plugin. Another service would just be wired into your module.services.yml instead of using the create() method:


namespace Drupal\my_module\Plugin\MyPlugin;

use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;

class MyPlugin extends implements ContainerFactoryPluginInterface {

   * Resoruce Manager service.
   * @var \Symfony\Component\HttpFoundation\RequestStack
  protected $requestStack;

   * {@inheritdoc}
  public function __construct(RequestStack $requestStack,
                              array $configuration,
                              $plugin_definition) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->requestStack = $requestStack;

   * {@inheritdoc}
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(

  function doSomething() {
    $request = $this->requestStack->getCurrentRequest();


And here's the Symfony documentation that basically says the same thing.

Your Answer

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

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