O'Reilly logo

Drupal 8 Module Development by Daniel Sipos

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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( ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required