# Environment in Slim 3 - Accessing the Slim instance

**URL:** <https://discourse.slimframework.com/t/environment-in-slim-3-accessing-the-slim-instance/427>\
**Category:** Questions\
**Created:** [June 22, 2016, 4:29pm UTC](https://discourse.slimframework.com/t/environment-in-slim-3-accessing-the-slim-instance/427 "2016-06-22T16:29:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eXist73](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@eXist73](https://discourse.slimframework.com/u/eXist73)\
**Post date:** [June 22, 2016, 4:29pm UTC](https://discourse.slimframework.com/t/environment-in-slim-3-accessing-the-slim-instance/427/1 "2016-06-22T16:29:03Z")

</div>

I’m trying to access the environment in Slim 3.

Specifically I’m trying to set some environment variables within my Middleware Class.

Is there a way to access the Slim instance in Slim 3 like there was in Slim 2?

For example in Slim 2 you could do something like:  
`$app = \Slim\Slim::getInstance();`

From there you could grab the environment:  
`$env = $app->environment();`

And then adjust environment variables accordingly:  
`$env['user'] = $user;`

Then later use this middleware pre-set environment variable in any route.

---

<div class="post-metadata">

**Author:** ![JoeBengalen](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@JoeBengalen](https://discourse.slimframework.com/u/JoeBengalen)\
**Post date:** [June 22, 2016, 8:59pm UTC](https://discourse.slimframework.com/t/environment-in-slim-3-accessing-the-slim-instance/427/2 "2016-06-22T20:59:41Z")

</div>

`$app = \Slim\Slim::getInstance();` was removed because it’s a bad practice.

If you need the `Slim\App` in your middleware you will have to inject it (via the constructor)

But seeing what you are after you don’t need the app instance. You could use the `Route` arguments to pass stuff from one middleware to another.

```php
/** @var $route \Slim\Route */
$route = $request->getAttribute('route'); 
$route->setArgument('user', $user);

// other middleware ...

/** @var $route \Slim\Route */
$route = $request->getAttribute('route'); 
$user = $route->getArgument('user');

```

---

<div class="post-metadata">

**Author:** ![eXist73](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@eXist73](https://discourse.slimframework.com/u/eXist73)\
**Post date:** [June 23, 2016, 5:31pm UTC](https://discourse.slimframework.com/t/environment-in-slim-3-accessing-the-slim-instance/427/3 "2016-06-23T17:31:23Z")

</div>

Gotcha, thanks.

So if I wanted to use the logger functionality within the middleware class, I would have to extend it as such ?

```
use Monolog\Logger;
class MyMiddleware
{
    protected $logger;

    public function __construct(Logger $logger)
    {
        $this->logger = $logger;
    }

    public function __invoke($request, $response, $next)
    {
        // Log the message
        $this->logger->critical('log info...');
    }
}
```

---

<div class="post-metadata">

**Author:** ![JoeBengalen](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@JoeBengalen](https://discourse.slimframework.com/u/JoeBengalen)\
**Post date:** [June 23, 2016, 9:40pm UTC](https://discourse.slimframework.com/t/environment-in-slim-3-accessing-the-slim-instance/427/4 "2016-06-23T21:40:34Z")

</div>

Not sure what you mean by ‘extend’, but yes, your example code seems correct.
