3

In a custom module, I have a custom service that uses GuzzleHttp\Client service.

mymodule.services.yml:

services:
  mymodule.mymodule_call:
    class: Drupal\mymodule\MyModuleCall
    arguments: ['@http_client']

The MyModuleCall is using dependency injection and start like:

namespace Drupal\mymodule;

use GuzzleHttp\Client;

class MyModuleCall {

  protected $client;

  public function __construct(Client $client) {
        $this->client  = $client;
  }

}

As explained in the GuzzleHttp documentation (http://docs.guzzlephp.org/en/latest/quickstart.html), we can pass an argument to the Client constructor to set the base_uri.

Like so:

use GuzzleHttp\Client;

$client = new Client([
    // Base URI is used with relative requests
    'base_uri' => 'http://httpbin.org'
]);

How do we set the 'base_uri' in the constructor when we use dependency injection to instantiate a class?

3
  • you don't provide the create method in your class?
    – Yuseferi
    Mar 28, 2017 at 21:05
  • Short answer: you can't. Arguments in your services.yml file are other services which are objects. You can add an extra setter in your class "setBaseUri" or put it in config and get it from the config factory
    – Robin
    Mar 29, 2017 at 6:40
  • There is a work around however...
    – mradcliffe
    Mar 29, 2017 at 13:18

1 Answer 1

3

While it would not be possible to use the http_client service it is possible to use the factory service (or create your own factory service if you choose not to rely on core methods).

The http_client_factory service provides a fromOptions method.

The Symfony2 documentation has a section on using parameters:

Use the parameters section of a config file to set parameters:

parameters:
  mymodule.http_client.config:
    base_uri: 'http://httpbin.org'

services:
  mymodule.http_client:
    factory: '@http_client_factory:fromOptions'
    arguments: ['%mymodule.http_client.config%']
  mymodule.mymodule_call:
    class: Drupal\mymodule\MyModuleCall
    arguments: ['@mymodule.http_client']

Now the MyModuleClass will use Guzzle with Drupal's default configuration and custom configuration.

Note: it is also possible to provide your own factory service for Guzzle and to use that directly. This would be useful if you need to use your own overridden Guzzle\Http\Client class (I do this in a contrib module). This would probably be necessary to inject custom configuration such as handler stack (OAuth1).

2
  • is it possible to pass an argument at runtime? for example, at the time of calling \Drupal::service('custom_service'); how to pass an argument?
    – Asit
    Jun 15, 2018 at 11:51
  • Get the factory service directly and call the appropriate method (or ideally a different method).
    – mradcliffe
    Jun 17, 2018 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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