Instantiate Controller with Interface not concrete class As background, I am rebuilding some old code and want to wrap all the curl based requests in an interface (HttpRequestInterface) so I can swap that out later without having to write all the api calls right now.

I am trying to pass the HttpRequestInterface into controller, but it keeps saying `


  TypeError: Argument 1 passed to
  Drupal\my_api\Controller\MyController::__construct() must be
  an instance of Drupal\my_api\Controller\HttpRequestInterface,
  instance of Drupal\my_api\Services\HttpRequest\CurlRequest given


I am new to Drupal from the .Net world and love how this functionality is out of the box and it seems like I am following the examples and code I find in core, but obviously I am missing something.

***One thing that sticks out is that this error message has the wrong namespace for HttpRequestInterface.

Below are the relevant files, I have only included what I think is pertinent to the issue, more can be made available.

Controller:

&lt;?php

namespace Drupal\my_api\Controller;

use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response;
use Drupal\my_api\Controller\BaseApiController;

/**
 * Controller routines for Api routes.
 */
class RallyBoundController extends BaseApiController {


`BaseController:

namespace Drupal\my_api\Controller;

use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\my_api\Services\HttpRequest\HttpRequestInterace;

/**
 * BaseApiController abstracts away common api concerns.
 */
abstract class BaseApiController extends ControllerBase {
  protected $httpService;

  /**
   * Contructor requires HttpRequest Service.
   *
   * @param HttpRequestInterface $httpService
   *   Implementation of HttpRequestInterface defined in services.yml.
   */
  public function __construct(HttpRequestInterface $httpService) {
    $this-&gt;httpService = $httpService;
  }

  /**
   * Get the HttpRequest implemention as interface so we can swap out as needed.
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container-&gt;get('my_api.httprequest')
    );
  }


Service.yml:

services:
  my_api.httprequest:
    class: Drupal\my_api\Services\HttpRequest\CurlRequest


HttpRequestInterface:

namespace Drupal\my_api\Services\HttpRequest;

/**
 * HttpRequestInterace defines interface for making Http GET Requests.
 */
interface HttpRequestInterface {


CurlRequest:

namespace Drupal\my_api\Services\HttpRequest;

use Drupal\my_api\Services\HttpRequest\HttpRequestInterface;

/**
 * HttpRequest Implementation of HttpRequestInterface using CURL.
 */
class CurlRequest implements HttpRequestInterface {


Thanks for the help.
In the BaseController file, you have a typo in the interface classname. Fix that and clear the cache. Your module namespace seems to be inconsistent too. Your custom classes should be namespaces under the same module name they are defined in.

use Drupal\my_api\Services\HttpRequest\HttpRequestInterace;

to

use Drupal\my_api\Services\HttpRequest\HttpRequestInterface;

PHP can’t figure out where the class is due to the typo, and expects it to be defined locally in the Controller class file, hence why your error message lists an incorrect namespace to the interface.