How to define your own Services in Drupal 8 – 9 | what is services in drupal 8 | what is services in drupal 9

Services is a simple PHP class, which is build for solving/achieving specific task/functionality throughout the application. Because of this it is very easy to use in the applicatins.

Service is a PHP class with some code that provides a single specific functionality throughout the application. So you can easily access each service and use its functionality wherever you need it. Because of that it is easy to test and configure in your application. This is called service-oriented architecture which is not unique to Symfony or even PHP.

Let’s see an example.

Step 1: You must have one custom module, if you don’t have then create custom module in drupal 8 – 9 first.

Step 2: Create file similar to routing file. name: module_name.services.yml file, here in content_api.services.yml and past the below code.

services:
 content_api.test:
   class: Drupal\content_api\HelloServices
  • Here the file name is ‘content_api.services.yml’ where the ‘content_api’ is our module name.
  • ‘content_api.test’ is the service name defined by us, , Where we need to follow the pattern of ‘module_name’ concatenate with a ‘unique_name’.
  • We have the class name for services. ‘class: Drupal\content_api\HelloServices’ which will be kept under the ‘src’ folder.

Step 3: Create the class ‘HelloServices.php’ under the ‘src’ folder, path: /web/modules/custom/content_api/src.

<?php
namespace  Drupal\content_api;

/**
* @file providing the service that generate random number between 1000 to 9999.
*
*/

class HelloServices {

 public function get_number()
 {
 	return rand(1000,9999);
 }
 
}

Step 4: access your own define services.

$service = \Drupal::service('content_api.test');    
echo $service->get_number();
Drupal services result
Drupal services result

Also, Read.

Let’s change little bit in content_api.services.yml file, added dependency injection.

services:
 content_api.test:
   class: Drupal\content_api\HelloServices
   arguments: ['@current_user']

here, added arguments: [‘@current_user’] as an arguments, to display name of current login user.

Access current_user in controller.

<?php
namespace  Drupal\content_api;

use Drupal\Core\Session\AccountInterface;
/**
* @file providing the service that generate random number between 1000 to 9999.
*
*/


class HelloServices {


 protected $currentUser;

 public function __construct(AccountInterface $currentUser) {

   $this->currentUser = $currentUser;
 }

 public function getData() {
    return $this->currentUser->getDisplayName();
  }
 

}

Now, access services.

 $service = \Drupal::service('content_api.test');   
 echo $service->getData();
Services call for current user login
Services call for current user login

Pradip Mehta

I am a well-organized professional in Drupal Development and PHP web development with strong script handling knowledge or automation process with PHP. I have advanced computer skills and am a proficient multitasker.