How to resolve my warning in my custom service? I am getting the following error:


  Warning: Missing argument 1 for Drupal\custom_forms\Service\CustomFormsService::__construct(), called in /drupal/docroot/core/lib/Drupal/Component/DependencyInjection/Container.php on line 264 and defined in Drupal\custom_forms\Service\CustomFormsService->__construct() (line 13 of modules/custom_forms/src/Service/CustomFormsService.php).


And my code (service.php) is:

public function __construct($request) {
$this-&gt;cityname = \Drupal::request()-&gt;get('cityname');

if($this-&gt;cityname != ''){
  $result = \Drupal::database()-&gt;select('city_municipality','cm')
    -&gt;fields('cm',array('municipality'))
    -&gt;condition('city', $this-&gt;cityname)
    -&gt;execute()-&gt;fetchAll();
    $result = json_encode($result);
    print $result; 
    exit;
 }


custom_forms_services.yml

  services:
       custom_forms.custom_forms_service:
           class: Drupal\custom_forms\Service\CustomFormsService


But the form functionalities are working fine.

Can anyone help me out on this?
You need to add an argument to your services.yml for the class to be injected:

services:
  custom_forms.custom_forms_service:
    class: Drupal\custom_forms\Service\CustomFormsService
    arguments:
      - @request_stack


Note that you should make a few changes to your service as well:

First, add an type hint for the argument:

public function __construct(\Symfony\Component\HttpFoundation\RequestStack $requestStack) {


Next, you will use the $requestStack instead of Drupal::request():

$this-&gt;cityname = $requestStack-&gt;getCurrentRequest()-&gt;query-&gt;get('cityname');