1

I face with a problem in defining service in drupal 8.0 my service is :

services:
  tracking.middleware:
    class: Drupal\mkt_tracking_v2\Middleware\TrackingMiddleware
    arguments: ['@kernel','@config.factory','@request_stack']
    tags:
      - {name: http_middleware, priority: 1000}

the problem is: the object that passes to TrackingMiddleware aren't based on the arguments definition.

as I get this error :

Thu Apr 04 01:51:23.919189 2019] [php7:notice] [pid 28435] [client 127.0.0.1:51962] TypeError: Argument 2 passed to Drupal\mkt_tracking_v2\Middleware\TrackingMiddleware::__construct() must implement interface Drupal\Core\Config\ConfigFactoryInterface, instance of Drupal\Core\DrupalKernel given, called in /home/hossein/Project/Drupal/8.7/web/core/lib/Drupal/Component/DependencyInjection/Container.php on line 274 in /home/hossein/Project/Drupal/8.7/web/modules/Marketing/mkt_tracking_v2/src/Middleware/TrackingMiddleware.php on line 41 #0 /home/hossein/Project/Drupal/8.7/web/core/lib/Drupal/Component/DependencyInjection/Container.php(274): Drupal\mkt_tracking_v2\Middleware\TrackingMiddleware->__construct(Object(Drupal\Core\StackMiddleware\NegotiationMiddleware), Object(Drupal\Core\DrupalKernel), Object(Drupal\Core\Config\ConfigFactory), Object(Symfony\Component\HttpFoundation\RequestStack))\n#1 /home/hossein/Project/Drupal/8.7/web/core/lib/Drupal/Component/DependencyInjection/Container.php(171): Drupal\Component\DependencyInjection\Container->createService(Array, 'tracking.middle...')\n#2 /home/hossein/Project/Drupal/8.7/web/core/lib/Drupal/Component/DependencyInjection/Container.php(480): Drupal\Component\DependencyInjection\Container->get('tracking.middle...', 1)\n#3 /home/hossein/Project/Drupal/8.7/web/core/lib/Drupal/Component/DependencyInjection/Container.php(230): Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters(Array)\n#4 /home/hossein/Project/Drupal/8.7/web/core/lib/Drupal/Component/DependencyInjection/Container.php(171): Drupal\Component\DependencyInjection\Container->createService(Array, 'http_kernel')\n#5 /home/hossein/Project/Drupal/8.7/web/core/lib/Drupal/Core/DrupalKernel.php(1412): Drupal\Component\DependencyInjection\Container->get('http_kernel')\n#6 /home/hossein/Project/Drupal/8.7/web/core/lib/Drupal/Core/DrupalKernel.php(693): Drupal\Core\DrupalKernel->getHttpKernel()\n#7 /home/hossein/Project/Drupal/8.7/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))\n#8 {main}, referer: http://drupal.local/admin/modules/uninstall but as i define the class in this way the constructor parameters is right :

 public function __construct(HttpKernelInterface $app,ConfigFactoryInterface $configFactory,RequestStack $requestStack) {
    $this->app = $app;
    $this->configFactory = $configFactory;
    $this->requestStack = $requestStack;
  }

did I do any mistake in service definition?

  • Did you have a previous version that had config factory as the first argument? Maybe the tag requires a full cache rebuild of the service container? – mradcliffe Apr 4 at 0:39
0

From the looks of the core middleware services, the HTTP kernel is automatically provided to the constructor. You don't have to specify it (and even if you did, @kernel is Drupal's kernel, so might not be what you meant).

As an example, ReverseProxyMiddleware's constructor signature:

public function __construct(HttpKernelInterface $http_kernel, Settings $settings) {

And its services entry:

http_middleware.reverse_proxy:
  class: Drupal\Core\StackMiddleware\ReverseProxyMiddleware
  arguments: ['@settings']
  tags:
    - { name: http_middleware, priority: 300 }

Note no explicit @kernel, yet it's the first constructor argument.

So in theory you just need to change you service definition and clear caches:

class: Drupal\mkt_tracking_v2\Middleware\TrackingMiddleware
  arguments: ['@config.factory','@request_stack']
  tags:
    - {name: http_middleware, priority: 1000}
-1

Note: use ConfigFactory class instead of ConfigFactoryInterface.

use Drupal\Core\Config\ConfigFactory;

public function __construct(HttpKernelInterface $app,ConfigFactory $configFactory,RequestStack $requestStack) {
    $this->app = $app;
    $this->configFactory = $configFactory;
    $this->requestStack = $requestStack;
  }
  • This is wrong, (and barely passable as an attempt to answer the question to be honest). For others’ reference... always use the interface when one is available; that’s the whole point of DI after all – Clive Apr 22 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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