Method getEnvironment / addGlobal deprecated

Hi. I’m new here and new to Slim in fact… I’m learning through here (minute 5:45)
But I found that this video was made in 2016 and when I try to emulate all of that, I got some deprecated functions and classes… so, I am kinda lost about how to implement this:

namespace App\Middleware;

class ValidationErrorsMiddleware extends Middleware
public function __invoke($request, $response, $next)
this->container->view->getEnvironment()->addGlobal('errors', _SESSION[‘errors’]);

    $response = $next($request, $response);
    return $response;


But, getEnvironment and addGlobals are deprecated… so, I search in slim framework page but even it’s mentioned I don’t get how to use other options to make this work…

Message: Call to undefined method Slim\Views\Twig::environment()
or if I delete getEnvironment
Message: Call to undefined method Slim\Views\Twig::addGlobal()

Do anyone could tell me what I need to use and where I can find information up today?

this are methods from Slim 2
Slim 3 is current version and 4 is in Beta

current docs can be found


if this does not help you, please provide your slim and twig view version from your composer file

Yes, I’m using Slim 3, but as I saw it, those are Slim 2 methods and now deprecated, but don’t know how to jump from Slim2 to Slim 3 with same functionality but not deprecated methods.

what versions exactly you are using? slim, slim twig view and Twig? (based on your composer file pls)

also please share your code how do you setup Twig View into your container

From Composer file:
“slim/slim”: “^3.0”,
“slim/twig-view”: “^2.4”,
“illuminate/database”: “^5.8”,
“doctrine/dbal”: “^2.9”,
“illuminate/console”: “^5.8”,
“phpmailer/phpmailer”: “^6.0”,
“slim/flash”: “^0.4.0”,
“respect/validation”: “^1.1”
When call composer info, Slim more accurate they are
slim/slim 3.12.1
slim/twig-view 2.5.0
twig/extensions v1.5.4
twig/twig v2.11.3

Yes, of course… this is in my app.php in my bootstrap directory

$container = $app->getContainer();

$container[‘view’] = function ($container) {
$view = new \Slim\Views\Twig(DIR . ‘/…/resources/views’, [
‘cache’ => false,
$view->addExtension(new \Slim\Views\TwigExtension(
return $view;

can you provide entire error message and backtrace?
this is weird; your error message is about undefined method environment()
but the method name (and your code) should be method getEnvironment()

Yes, sorry… I try so many times to understand the error, that I copied environment() once instead of getEnvironment().

Anyway searching I found a way to bypass the problem, using the TwigExtension like here

$view->parserExtensions = array(
new \Slim\Views\TwigExtension(),
$app->view->setTemplatesDirectory( dirname(FILE) . ‘/Views’);

$twig = $app->view()->getEnvironment();
$twig->addGlobal(‘test’, ‘Slim Awesomeness!’);

I am a little concern to move from Slim 2 to 3 (then to 4) with this course, wish I could find the correct way. Thanks for the help @jDolba

In Slim 4:

$twig->addGlobal('test', 'Slim Awesomeness!');


$twig->getEnvironment()->addGlobal('test', 'Slim Awesomeness!');