官术网_书友最值得收藏!

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(
$container->get('hello_world.salutation')
);
}

In addition to this, ensure that you include the relevant use statements at the top of the file:

use Drupal\hello_world\HelloWorldSalutation;
use Symfony\Component\DependencyInjection\ContainerInterface;

So, what is going on here? First, we give the Controller a constructor method, which takes our service as an argument and stores it as a property. For me, this is usually the very first method in the class, but how does this constructor get its argument? It gets it via the create() method, which receives the Service Container as a parameter and is free to choose the service(s) needed by the Controller constructor. This is usually my second method in a class. I prefer this order because it's very easy to check whether they are present. Also, their presence is important, especially when inheriting and observing what the parent is injecting, but how does this injection business work in reality?

In a nutshell, after the route is found and the responsible Controller is resolved, a check is made to see whether the latter implements ContainerInjectionInterface. Our Controller does so via its parent ControllerBase. If it does, the Controller gets instantiated via the create() method and the container is passed to it. From there, it is responsible for creating a new static version of itself with the required services from the container--not that complicated, really!

The create() method is a staple practice in the Drupal 8 dependency injection pattern, so you will see it quite a lot. However, one thing to keep in mind is that you should never pass the entire container to the class you instantiate with it because you are no longer doing dependency injection then.

A note about ControllerBase, which we are extending--it is a standard practice to extend it. It provides some nice traits, implements interfaces that are required and shows what the class purpose is immediately. However, from the point of view of dependency injection, I advise against using the helper methods that return services (for example, entityManager()). They, unfortunately, load services statically, which is not the best practice in this case. You should instead inject them yourself as we did earlier in this chapter.

Okay, now to turn back to our example. Now that we have the service injected, we can use it to render the dynamic salutation:

    return [
'#markup' => $this->salutation->getSalutation(),
];

There we have it. Now, our greeting is dependent on the time of day and our Controller is dependent on our salutation service.

One thing I would like to specify about our example is that I disregarded caching for the sake of simplicity. With the cache turned on, the page would be cached and served with potentially the wrong salutation. However, we'll have an entire chapter dedicated to caching, where we will cover all these intricacies, so there is no point in complicating our example.

主站蜘蛛池模板: 抚宁县| 丰都县| 丹凤县| 瑞安市| 淄博市| 上高县| 博白县| 祁门县| 报价| 武夷山市| 靖远县| 五华县| 栾城县| 岳阳市| 大宁县| 萝北县| 蓬莱市| 翁牛特旗| 沂南县| 金沙县| 循化| 浦东新区| 河曲县| 宁夏| 乐平市| 镇赉县| 桐乡市| 湖州市| 敖汉旗| 怀来县| 页游| 京山县| 花莲市| 辉县市| 莱阳市| 安福县| 梅州市| 杭锦旗| 遂平县| 祁阳县| 绥江县|