- Drupal 8 Module Development
- Daniel Sipos
- 403字
- 2021-07-02 12:22:39
Using services in Drupal 8
Before we go and use our service in the Controller we created, let's take a breather and run through the ways you can make use of services once they are registered.
There are essentially two ways—statically and injected. The first is done by a static call to the Service Container, whereas the second uses dependency injection to pass the object through the constructor (or in some rare cases, a setter method). However, let's check out how, why, and what is the real difference.
Statically, you would use the global Drupal class to instantiate a service:
$service = \Drupal::service('hello_world.salutation');
This is how we use services in the .module files and classes which are not exposed to the Service Container and into which we cannot inject. Instances of the latter are rare though, most of the time we use the static calls only from within static contexts.
A few popular services also have shorthand methods on the \Drupal class: for example, \Drupal::entityTypeManager(). I recommend that you inspect the \Drupal class and take a look at the ones with shorthand methods available.
It is not best practice and, for me, it is personally unacceptable to use the static method of service instantiation inside a Controller, service, plugin or any other class where dependency injection is an option. The reason is that it defeats much of the purpose of using a service, as it couples the two, making it a nightmare to test. Inside hook implementations and other Drupal-specific procedural code, on the other hand, we have no choice, and it is normal to do so.
The proper way to use services is to inject them where needed. Admittedly, this approach is a bit more time-consuming but, as you progress, it will become second nature. Also, since there are a few different ways to inject dependencies (based on the receiver), we will not cover them here. Instead, we will see how they work throughout this book, at the right time. We will take a look at a very important example right now in the next section.
- 小程序?qū)崙?zhàn)視頻課:微信小程序開發(fā)全案精講
- 你必須知道的204個(gè)Visual C++開發(fā)問題
- SQL基礎(chǔ)教程(視頻教學(xué)版)
- Big Data Analytics
- Kinect for Windows SDK Programming Guide
- 青少年P(guān)ython編程入門
- Python圖形化編程(微課版)
- Anaconda數(shù)據(jù)科學(xué)實(shí)戰(zhàn)
- AMP:Building Accelerated Mobile Pages
- 程序員的成長課
- Getting Started with Web Components
- Offer來了:Java面試核心知識點(diǎn)精講(框架篇)
- 絕密原型檔案:看看專業(yè)產(chǎn)品經(jīng)理的原型是什么樣
- 開源網(wǎng)絡(luò)地圖可視化:基于Leaflet的在線地圖開發(fā)
- 流暢的Python