Environment in Slim 3 - Accessing the Slim instance

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.

$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.

/** @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');

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...');
    }
}

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