Injecting the service into our Controller

We have now closed the parenthesis on how services can be used. Let's take a look at how to inject our newly created service into our Controller.

We will need to add some code to our Controller (typically at the beginning of the class so that we can immediately identify the presence of this code when looking at it):

 /**   * @var \Drupal\hello_world\HelloWorldSalutation   */  protected $salutation;  /**   * HelloWorldController constructor.   *   * @param \Drupal\hello_world\HelloWorldSalutation $salutation   */  public function __construct(HelloWorldSalutation $salutation) {    $this->salutation = $salutation;  }  /**   * {@inheritdoc}   */  public static function create(ContainerInterface $container) { return new static( ...

Get Drupal 8 Module Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.