# How get DI container into middleware in Slim 4

**URL:** <https://discourse.slimframework.com/t/how-get-di-container-into-middleware-in-slim-4/3752>\
**Category:** Questions\
**Created:** [December 25, 2019, 5:59am UTC](https://discourse.slimframework.com/t/how-get-di-container-into-middleware-in-slim-4/3752 "2019-12-25T05:59:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![prohor\_iofe](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/prohor_iofe/32/1050_2.png) [@prohor\_iofe](https://discourse.slimframework.com/u/prohor_iofe)\
**Post date:** [December 25, 2019, 5:59am UTC](https://discourse.slimframework.com/t/how-get-di-container-into-middleware-in-slim-4/3752/1 "2019-12-25T05:59:00Z")

</div>

Hi all. I am start learn Slim 4. I want to save old data from my form to session (for render in twig template). In Slim 3 I can get DI container into middleware like this  
[https://prnt.sc/qf7n7x](https://prnt.sc/qf7n7x)  
But in Slim 4 I can’t do it. I don’t have DI container in $this(middleware)  
How I can get DI container in my middleware?

```auto
class OldInputMiddleware
{
    /**
     * Example middleware invokable class
     *
     * @param ServerRequest $request PSR-7 request
     * @param RequestHandler $handler PSR-15 request handler
     *
     * @return Response
     */
    public function __invoke(Request $request, RequestHandler $handler): Response
    {

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

       /*
        * How get DI this?
        */
        return $response;
    }
}

```

---

<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:** [December 25, 2019, 12:31pm UTC](https://discourse.slimframework.com/t/how-get-di-container-into-middleware-in-slim-4/3752/2 "2019-12-25T12:31:12Z")

</div>

Instead of fetching the objects from the container, you can (should) let the DI container inject the declared dependencies into your objects via (constructor) [dependency injection](http://php-di.org/doc/understanding-di.html). The container itself should not be injected.

**Example** :

```php
<?php

namespace App\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

final class SessionMiddleware implements MiddlewareInterface
{
    /**
     * @var SessionInterface
     */
    private $session;

    public function __construct(SessionInterface $session)
    {
        $this->session = $session;
    }

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        if (!$this->session->isStarted()) {
            $this->session->start();
        }

        $response = $handler->handle($request);
        $this->session->save();

        return $response;
    }
}

```

---

<div class="post-metadata">

**Author:** ![prohor\_iofe](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/prohor_iofe/32/1050_2.png) [@prohor\_iofe](https://discourse.slimframework.com/u/prohor_iofe)\
**Post date:** [December 25, 2019, 1:34pm UTC](https://discourse.slimframework.com/t/how-get-di-container-into-middleware-in-slim-4/3752/3 "2019-12-25T13:34:27Z")

</div>

> [@odan](#):
>
> final class SessionMiddleware implements MiddlewareInterface

Great! Very clearly! I will do it! Thanks.
