Drupal 8 is a Great Way of Developing a Useful and Valuable Website – Part Ten


Sharing is caring!

In the web technology world, things are changing constantly, and a developer must move with the changes, or else he/she will be left behind indeed. It is a good idea for a good developer, to enhance their skills, in order to be on top of their game.

In today’s article we are going to be talking a bit about controllers, which is another Drupal 8 buzz word. Controllers have been designed and registered to handle requests and generate responses. The controller is where the developer shine because the website page is developed in the controller function, which are very simple.

You can connect to a database, query the database or even send an email from your controller file. Controller functions must return a Symphony response object, which is the one big rule about controller functions. The router controller response flow, will allow us to understand how symphony works and Drupal 8 in general works.

Services are also part of symphony, which is part of Drupal; services are found in the service container. Services do something for the user, by them accessing the service container; knowing how to use and access the service container is very important. When a developer uses services he/she is not required to do things one way, because services are flexible. Once you are very familiar with a service, you should be able to request a service from the service container and use it in your code to do things such as send an email.

You should also be able to define a new service, override an existing service and subscribe to an event. Service objects don’t interrupt the code that is being used to call a service, when they implement an interface or are exchanged for the services which implement the same interface. The ability to use a third party send/receive email services or be able to work with multiple different database servers is given to Drupal 8 through services.

The functionalities that services have to offer, can only be accessed by a Drupal 8 developer, by accessing the service container. The decoupled nature of the Drupal 8 systems must be respected; therefore, services must be accessed through a service container. A developer who want to use services, must know what services exist, as well as learn their machine name, so that they can request a copy of said service from the service container.

Through the dependency Injection Container, services can be collected, modules can be used to override services to use different instantiation or different classes. Developers can view services provided by contributed modules or those services from core, by generating a list of services for the current site, which can be obtained, through the Drupal Console.

A developer would use this code, to get a complete list of the services available, drupal debug:container, to get a list of services filtered, then the grep command would be used, in this manner, drupal debug:container | grep path.alias_manager.

If you as a developer want to make your life easier, have a strong coding architecture, and be able to centralize the way that services are used, it is best practice to use the service container. We are going to close our article now on services, but I am sure we are going to be picking this topic back up in the future, especially if it’s hard to understand.

Thank you for reading this article!!!