# Request object in DI container

**URL:** https://discourse.slimframework.com/t/request-object-in-di-container/5481
**Category:** Questions
**Created:** [January 1, 2023, 11:07pm UTC](https://discourse.slimframework.com/t/request-object-in-di-container/5481 "2023-01-01T23:07:11Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Alpine418](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@Alpine418](https://discourse.slimframework.com/u/Alpine418)
#### Post date: [January 1, 2023, 11:07pm UTC](https://discourse.slimframework.com/t/request-object-in-di-container/5481/1 "2023-01-01T23:07:12Z")

</div>

Hi,

I need the request object (PSR-7, ServerRequestInterface) in my DI container. How can I add and use it? The request object will be handled through the middlewares and delivered as argument to the closure or controller action of the matchting route. But I need it for a dependency of the URI extension of Plates.

> **[URI | Plates](https://platesphp.com/extensions/uri/)**
>
> Plates is a Twig inspired, native PHP template system that brings modern template language functionality to native PHP templates.

Can anybody help me?

Thanks.

---

<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: [January 2, 2023, 10:43am UTC](https://discourse.slimframework.com/t/request-object-in-di-container/5481/2 "2023-01-02T10:43:43Z")

</div>

You can do this by implementing a custom Middlware that adds the Plates URI extension to the engine.

Here is an example:

```php
<?php

namespace App\Middleware;

use League\Plates\Engine;
use League\Plates\Extension\URI;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

final class PlatesMiddleware implements MiddlewareInterface
{
    private Engine $engine;

    public function __construct(Engine $engine)
    {
        $this->engine = $engine;
    }

    public function process(
        ServerRequestInterface $request,
        RequestHandlerInterface $handler
    ): ResponseInterface {
        // Load URI extension using a PSR-7 request object
        $this->engine->loadExtension(new URI($request->getUri()->__toString()));

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

```

Add the PlatesMiddleware before the RoutingMiddleware.

```php
use App\Middleware\PlatesMiddleware;

$app->add(PlatesMiddleware::class);
$app->addRoutingMiddleware();

```

This example assumes that you have installed a DI container, e.g. PHP-DI.

---

<div class="post-metadata">

### Author: ![Alpine418](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@Alpine418](https://discourse.slimframework.com/u/Alpine418)
#### Post date: [January 3, 2023, 9:24pm UTC](https://discourse.slimframework.com/t/request-object-in-di-container/5481/3 "2023-01-03T21:24:08Z")

</div>

You are my hero! Thank you.

So there is no solution to get the request object into the DI container because of the application lifecycle?

---

<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: [January 4, 2023, 4:16pm UTC](https://discourse.slimframework.com/t/request-object-in-di-container/5481/4 "2023-01-04T16:16:28Z")

</div>

You’re welcome 🙂

The Request and Response objects are context-specific and therefore have a different life cycle. These two objects should (must) therefore never be in the DI container.
