how to access configuration module from service I have module config, something like:

modulename.config.yml:

modulename:
  core_address: 107.101.xx.yy
  core_port: 153759
  core_user: superuser
  core_pass: superpass


And I have module service, something like:

modulename.service.yml:

services:
    modulename.my_service:
        class: Drupal\modulename\group\MyService


MyService.php:

/**
 * @file
 * Contains \Drupal\modulename\group\MyService.
 */

namespace Drupal\modulename\group;

/**
 * MyService.
 */
class MyService{

  public function __construct() { }

  public function getDemoValue() {
    $config =  \Drupal::config('modulename.my_service');
    $core_port = $config-&gt;get('modulename.core_port');
    return $core_port. 'asdf';
  }

}


problem is:

that's $core_port return empty value...

and the question is:

how i can get access (read/write) my config from my module service?

not sure if this relevant, but getDemoValue() is called from different module as follow:

/**
 * @file
 * Contains \Drupal\anothermodulename\Controller\MyForm.
 */

namespace Drupal\anothermodulename\Controller;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\UrlHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * MyForm.
 */
class MyForm extends FormBase {

  protected $demoService;

  /**
   * Class constructor.
   */
  public function __construct($demoService) {
    $this-&gt;demoService = $demoService;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container-&gt;get('modulename.my_service')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() { /** nothing interesting here **/ }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    /** nothing interesting here **/
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &amp;$form, FormStateInterface $form_state){ /** nothing interesting here **/ }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &amp;$form, FormStateInterface $form_state) {
    // Display result.
    foreach ($form_state-&gt;getValues() as $key =&gt; $value) {
      drupal_set_message($key . ': ' . $value . '  ' . $this-&gt;demoService-&gt;getDemoValue()); // i try to print here but didn't happend
    }
  }

}
To read the variables (configs) you'd do this:

$config = \Drupal::config('modulename.my_service');

$core_port = $config-&gt;get('modulename.core_port');


To write to the variables (configs) you'd do this:

// Set and save new message value.

$config = \Drupal::service('config.factory')-&gt;getEditable('modulename.my_service');
$config-&gt;set('modulename.core_port', 153760)-&gt;save();


There's an example you can look at in the core module aggregator.

It has a settings form at /core/modules/aggregator/src/Form/SettingsForm.php

and then it adds a route to access the settings form:

aggregator.admin_settings:
  path: '/admin/config/services/aggregator/settings'
  defaults:
    _form: '\Drupal\aggregator\Form\SettingsForm'
    _title: 'Feed aggregator settings'
  requirements:
    _permission: 'administer news feeds'


You could pretty much copy the form into your module and change the variables to match your config.

Generally you'll want to add the config in your service constructor.

namespace Drupal\modulename;

/**
 * Example Service with config.
 */
class MyService {


  /**
   * Drupal's settings manager.
   */
  protected $settings;

  /**
   * Constructor.
   */
  public function __construct() {
    $this-&gt;settings = \Drupal::config('modulename.my_service');
  }

}


Then you'd access the variables like this:

$this-&gt;settings-&gt;get('modulename.core_port');