Score:1

Why is the service “@url_helper” not found?

tr flag

I am trying to add the query string with the each URL access through the browser. To accomplish it, trying to implement that through the service. But getting no solution in Drupal 9.

mymodule.services.yml:

    services:
      weplant.userlibraryurl:
      class: Drupal\weplant\EventSubscriber\UserLibrarySubscriber    
      arguments: ['@url_helper', '@request_stack']
      tags:
        - { name: event_subscriber }

UserLibrarySubscriber.php

<?php
namespace Drupal\weplant\EventSubscriber;

use Drupal\Core\Url;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class UserLibrarySubscriber implements EventSubscriberInterface {

  /**
   * The URL Helper service.
   *
   * @var \Drupal\Core\UrlHelper
   */
  protected $urlHelper;

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * Constructs a new instance.
   *
   * @param \Drupal\Core\UrlHelper $url_helper
   *   The URL helper service.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   */
  public function __construct(UrlHelper $url_helper, RequestStack $request_stack) {
    $this->urlHelper = $url_helper;
    $this->requestStack = $request_stack;
  }

  /**
   * Modifies the URLs before they are processed.
   */
  public function modifyUrls(GetResponseEvent $event) {
    $request = $event->getRequest();
    $current_url = Url::fromUri($request->getUri());
    $query_params = $current_url->getOption('query') ?? [];
    $query_params['my_param'] = 'my_value';
    $current_url->setOption('query', $query_params);
    $modified_url = $this->urlHelper->toString($current_url);
    $request->server->set('REQUEST_URI', $modified_url);
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = ['modifyUrls'];
    return $events;
  }
}

?>

Error:

The website encountered an unexpected error. Please try again later.
Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: The service "weplant.userlibraryurl" has a dependency on a non-existent service "url_helper". in Symfony\Component\DependencyInjection\Compiler\CheckExceptionOnInvalidReferenceBehaviorPass->processValue() (line 86 of /Users/test/Sites/auth8/vendor/symfony/dependency-injection/Compiler/CheckExceptionOnInvalidReferenceBehaviorPass.php).
Symfony\Component\DependencyInjection\Compiler\CheckExceptionOnInvalidReferenceBehaviorPass->processValue(Object, ) (Line: 83)
Symfony\Component\DependencyInjection\Compiler\AbstractRecursivePass->processValue(Array, ) (Line: 49)
Symfony\Component\DependencyInjection\Compiler\CheckExceptionOnInvalidReferenceBehaviorPass->processValue(Array) (Line: 92)
Symfony\Component\DependencyInjection\Compiler\AbstractRecursivePass->processValue(Object, 1) (Line: 49)
Symfony\Component\DependencyInjection\Compiler\CheckExceptionOnInvalidReferenceBehaviorPass->processValue(Object, 1) (Line: 83)
Symfony\Component\DependencyInjection\Compiler\AbstractRecursivePass->processValue(Array, 1) (Line: 49)
Symfony\Component\DependencyInjection\Compiler\CheckExceptionOnInvalidReferenceBehaviorPass->processValue(Array, 1) (Line: 47)
Symfony\Component\DependencyInjection\Compiler\AbstractRecursivePass->process(Object) (Line: 40)
Symfony\Component\DependencyInjection\Compiler\CheckExceptionOnInvalidReferenceBehaviorPass->process(Object) (Line: 94)
Symfony\Component\DependencyInjection\Compiler\Compiler->compile(Object) (Line: 762)
Symfony\Component\DependencyInjection\ContainerBuilder->compile() (Line: 1339)
Drupal\Core\DrupalKernel->compileContainer() (Line: 943)
Drupal\Core\DrupalKernel->initializeContainer() (Line: 482)
Drupal\Core\DrupalKernel->boot() (Line: 711)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
id flag
I’ve answered the question as to why your service definition does not work.
Score:3
us flag

The url_helper service does not exist; that is why you get that error.
As the other answer said, there is the UrlHelper class, which is a class that implements static methods, and cannot be used to implement a service.

You do not need to use any method of that class, though. Since you already have a Url instance, you just need to call its toString() method.

public function modifyUrls(GetResponseEvent $event) {
  $request = $event->getRequest();
  $current_url = Url::fromUri($request->getUri());
  $query_params = $current_url->getOption('query') ?? [];
  $query_params['my_param'] = 'my_value';
  $current_url->setOption('query', $query_params);
  $request->server->set('REQUEST_URI', $current_url->toString());
}

(I will not comment on whether that is the correct code to use in that case.)

As a side note, you can verify which services are implemented by Drupal core by reading the content of some .services.yml files (whose content is also shown on api.drupal.org): The services that are part of Drupal core, but that are not implemented by a Drupal core module, are listed in core.services.yml; the services implemented by a Drupal core module are listed in a <module machine name>.services.yml file, for example user.services.yml (for the User module).

Score:1
id flag

UrlHelper is not a service. It is a utility class with public static methods. Remove it from the arguments key in the custom service definition and remove it from the class constructor.

To use it, call static methods like UrlHelper::someExistingMethod. But toString, which you are trying to call on UrlHelper, does not exist.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.