4

I've been following instructions on the web on creating a new service, but I can't seem to get it to work.

In my controller:

namespace Drupal\mymodule\Controller;

use Drupal\Core\Controller\ControllerBase;

class MyController extends ControllerBase {
  public function myroute() {
      $db = \Drupal::service('mymodule.db');
      return array('DEBUG');
  }
} 

In my mymodule.services.yml:

services:
  mymodule.db:
    class: Drupal\mymodule\mymoduleDB

In my service class:

namespace Drupal\mymodule;

class mymoduleDB {
  public function dosomething() {
    return 'data...';
  }
}

In my controller, when I comment out the line that calls \Drupal::service, then the controller works, so it's an issue with calling the service.

I'm getting a HTTP 500 error.

0

3 Answers 3

11

Create mymodule.services.yml

services:
  mymodule.custom_services:
    class: Drupal\mymodule\Services\CustomService
    arguments: []

Now create Services folder under your mymodule/src. After that create CustomService.php in Service folder.

In CustomService.php

<?php

namespace Drupal\mymodule\Services;

/**
 * Class CustomService.
 */
class CustomService {

  /**
   * Constructs a new CustomService object.
   */
  public function __construct() {

  }

  public function getServiceData() {
    //Do something here to get any data.
  }
  /**
   * Here you can pass your values as $array.
   */
  public function postServiceData($array) {
    //Do something here to post any data.
  }
}

And for accessing service in your module file

$service = \Drupal::service('mymodule.custom_services');
$service->getServiceData(); 
2
  • 1
    I just created a service in my module and I didn't have to create a directory named Services. I also just searched all my codebase, and a directory named Services is nowhere to be found. What was your source?
    – Kartagis
    Mar 25, 2019 at 7:18
  • @Kartagis that's just personal preference, as long as it's in the src folder and the paths are correct.
    – ProGrammar
    Nov 20, 2019 at 18:28
2

Use dependency injection

namespace Drupal\mymodule\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
class MyController extends ControllerBase implements ContainerInjectionInterface{
  protected $moduleDB;

  /**
   * Constructs a MyController object
   *
   *   The module handler service.
   */
  public function __construct($moduleDB) {
    $this->moduleDB = $moduleDB;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('mymodule.db')
    );
  }

  public function myroute() {
    $db = $this->moduleDB;
    return array('DEBUG');
  }
}
0

The answer from A Reddy was pretty much what I had set up, so I stripped the code further. I had a line where I was calling:

$resp = new stdClass();

I removed it and it works. I need to research why this doesn't work, but my original setup was correct.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.