2

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.

closed as too broad by kiamlaluno Dec 11 '18 at 19:56

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

8

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(); 
  • 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 at 7:18
  • @Kartagis that's just personal preference, as long as it's in the src folder and the paths are correct. – ProGrammar yesterday
1

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.

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