# I need to get request headers in a dependency of container. I’m using PHP-DI

**URL:** https://discourse.slimframework.com/t/i-need-to-get-request-headers-in-a-dependency-of-container-i-m-using-php-di/5268
**Category:** Questions
**Created:** [May 20, 2022, 1:51pm UTC](https://discourse.slimframework.com/t/i-need-to-get-request-headers-in-a-dependency-of-container-i-m-using-php-di/5268 "2022-05-20T13:51:17Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![saronlujan](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@saronlujan](https://discourse.slimframework.com/u/saronlujan)
#### Post date: [May 20, 2022, 1:51pm UTC](https://discourse.slimframework.com/t/i-need-to-get-request-headers-in-a-dependency-of-container-i-m-using-php-di/5268/1 "2022-05-20T13:51:17Z")

</div>

Is it possible to get the request inside the container? I want to get the language of the user sent to each request. Picking up the controller and going over it with each call I found it a lot of work…

```auto
$container->set('language', function (// how get request here) {
    return new App\language\Language($request->getHeaderLine('Accept-Language'));
});

```

Tanks…

---

<div class="post-metadata">

### Author: ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)
#### Post date: [May 20, 2022, 6:33pm UTC](https://discourse.slimframework.com/t/i-need-to-get-request-headers-in-a-dependency-of-container-i-m-using-php-di/5268/2 "2022-05-20T18:33:14Z")

</div>

This is not recommended anymore because the request is context specific.  
Instead you may try to use the request attributes like this:

```php
$request = $request->withAttribute('my-key', 'my-value');

// 'my-value'
$value = $request->getAttribute('my-key');

```

If you want to set the language for a translator (for example), you may use a specific middleware to that fetches the language from the request and sets it into the translator. All this depends on your specific use case.

---

<div class="post-metadata">

### Author: ![saronlujan](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@saronlujan](https://discourse.slimframework.com/u/saronlujan)
#### Post date: [May 20, 2022, 6:57pm UTC](https://discourse.slimframework.com/t/i-need-to-get-request-headers-in-a-dependency-of-container-i-m-using-php-di/5268/3 "2022-05-20T18:57:57Z")

</div>

```auto
<?php namespace App\middlewares;

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;

class LanguageMiddleware {

    public function __invoke(Request $request, RequestHandler $handler): Response {
        $language = $request->getHeaderLine('Accept-Language'); 

        $request = $request->withAttribute('language', $language);
        $response = $handler->handle($request);

        return $response;
    }
}

```

in controller

```auto
$language = $request->getAttribute('language')

```

I arrived and ate here. My middleware receives and detects the language. I create an attribute that can be retrieved in. controller. The problem is that I always need to pass it on to my service. I wanted to put the language inside the. container for me to receive directly in my service and use with annotations.

---

<div class="post-metadata">

### Author: ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)
#### Post date: [May 21, 2022, 9:29am UTC](https://discourse.slimframework.com/t/i-need-to-get-request-headers-in-a-dependency-of-container-i-m-using-php-di/5268/4 "2022-05-21T09:29:29Z")

</div>

Ok the middleware looks fine. Without knowing more about your application, have you tried to change the language of your “translator” component there too? Then you may not need to pass the language everywhere and keep to focus more where it belongs to.

This example requires PHP-DI with Autowiring enabled (by default):

```php
<?php

namespace App\middlewares;

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use Acme\Translator;

final class LanguageMiddleware implements MiddlewareInterface
{
    private Translator $translator;

    public function __construct(Translator $translator)
    {
        $this->translator = $translator;
    }

    public function __invoke(Request $request, RequestHandler $handler): Response
    {
        $language = $request->getHeaderLine('Accept-Language'); 

        if ($language) {
            // pseudo example
            $this->translator->setLanguage($language);
        }

        return $handler->handle($request);
    }
}

```

---

<div class="post-metadata">

### Author: ![saronlujan](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@saronlujan](https://discourse.slimframework.com/u/saronlujan)
#### Post date: [May 21, 2022, 3:10pm UTC](https://discourse.slimframework.com/t/i-need-to-get-request-headers-in-a-dependency-of-container-i-m-using-php-di/5268/5 "2022-05-21T15:10:10Z")

</div>

> [@odan](#):
>
> ```auto
> if ($language) {
> // pseudo example
> $this->translator->setLanguage($language);
> }
> 
> ```

Hello, tanks for you help!  
I could not retrieve the “translator” in the service…

Don’t give up :D. If so, I’ll make the code available on github. It seems to be something simple… but I’m a few days into it.

---

<div class="post-metadata">

### Author: ![saronlujan](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@saronlujan](https://discourse.slimframework.com/u/saronlujan)
#### Post date: [May 25, 2022, 8:23pm UTC](https://discourse.slimframework.com/t/i-need-to-get-request-headers-in-a-dependency-of-container-i-m-using-php-di/5268/6 "2022-05-25T20:23:38Z")

</div>

```auto
<?php namespace App\middlewares;

use DI\Container;

class LanguageMiddleware {

    protected $container;

    public function __construct(Container $container){
        return $this->container = $container;
    }

    public function __invoke($request, $handler) {

        $this->container->set('language', function() use ($request){
            return new App\language\Language($request->getHeaderLine('Accept-Language'));
        });

        $response = $handler->handle($request);
        return $response;
    }
}

```

in my service

```auto
/**
* @Inject("language")
*/
private $language;

```

So it works well, I can pass the language in the constructor of the Language class and retrieve it later by Annotation in the service… not needing to go through the controller. Is this a good solution?
