Correct way to call a service in a controller I'm new to services (and drupal 8!) and trying to figure out services. I think perhaps I'm just doing the create in an outdated way. What I want do is make my 'hello generator' a service and call it in another controller with the following:

DBController.php

namespace Drupal\db\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\db\DbServices\HelloGenerator;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response;


class DBController extends ControllerBase
{
private $helloGenerator;

public function __construct(HelloGenerator $x) {
    $this-&gt;helloGenerator = $x;
}
public function say($count) {
    $hello = $this-&gt;helloGenerator-&gt;getHello($count);

    return new Response($hello);
}

public static function create(ContainerInterface $container) {
    $x = $container-&gt;get('db.hello_generator');
    return new static ($x);
}
}


HellGenerator.php

namespace Drupal\db\DbServices;

class HelloGenerator {

public function getHello($count) {
    $foo = 4 + 4 + 4;
    return $foo . ' ' . $count;
}

}


db.services.yml

services:
  db.hello_generator:
     class: Drupal\db\DbServices\HelloGenerator

db.db_says:
  path: /db/says/{count}
  defaults:
    _controller: '\Drupal\db\Controller\DBController::say'
  requirements:
    _permission: 'access content'
There are 3 questions here:


How to create a controller?
How to create a service?
How to use services in a controller?  


1. Create a controller

1.1 Add a controller class - you did it. For others see Drupal documentation

1.2 Add a routing file - it looks like this is missing in your code:

my_module.routing.yml

my_module.my_route_name:
  path: '/my/path'
  defaults:
    _controller: '\Drupal\my_module\Controller\MyController::myAction'
  requirements:
    _permission: 'access content'


see Drupal documentation

2. Create a service

2.1 Add a service class - you did it.

2.2 Add a routing file - you did it although it contains extra code that belongs to the routing file. This is all you need:

my_module.services.yml

services:
  my_module.my_service:
    class: Drupal\my_module\Service\MyService


see Drupal documentation

3. Use services in a controller

You did it. Here's a reference for others:

3.1 Override the create() method

The controller class inherits this method from class ControllerBase. Its role is to create the controller object. Watch the method argument: it is passed the services container. Extract the services you need and pass them to the constructor:

public static function create(ContainerInterface $container) {
    $my_service = $container-&gt;get('my_module.my_service');
    $other_service = $container-&gt;get('other_service');

    return new static($my_service, $other_service);
}


3.2 Initialise services in __construct()

public function __construct($my_service, $other_service)
{
    $this-&gt;my_service = $my_service;
    $this-&gt;other_service = $other_service;
}


3.3 Start using the services