# Method getEnvironment / addGlobal deprecated

**URL:** <https://discourse.slimframework.com/t/method-getenvironment-addglobal-deprecated/3265>\
**Category:** Questions\
**Created:** [July 11, 2019, 6:19pm UTC](https://discourse.slimframework.com/t/method-getenvironment-addglobal-deprecated/3265 "2019-07-11T18:19:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aikidosama](https://avatars.discourse-cdn.com/v4/letter/a/9d8465/32.png) [@aikidosama](https://discourse.slimframework.com/u/aikidosama)\
**Post date:** [July 11, 2019, 6:19pm UTC](https://discourse.slimframework.com/t/method-getenvironment-addglobal-deprecated/3265/1 "2019-07-11T18:19:48Z")

</div>

Hi. I’m new here and new to Slim in fact… I’m learning through here [https://www.youtube.com/watch?v=0hKciR\_dJAk&list=PLfdtiltiRHWGc\_yY90XRdq6mRww042aEC&index=12](https://www.youtube.com/watch?v=0hKciR_dJAk&list=PLfdtiltiRHWGc_yY90XRdq6mRww042aEC&index=12) (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’]);  
unset($\_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?

---

<div class="post-metadata">

**Author:** ![jDolba](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/jdolba/32/815_2.png) [@jDolba](https://discourse.slimframework.com/u/jDolba)\
**Post date:** [July 11, 2019, 9:25pm UTC](https://discourse.slimframework.com/t/method-getenvironment-addglobal-deprecated/3265/2 "2019-07-11T21:25:48Z")

</div>

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

current docs can be found  
[http://www.slimframework.com/docs/](http://www.slimframework.com/docs/)

github:

> **[slimphp/Slim](https://github.com/slimphp/Slim)**
>
> Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs. - slimphp/Slim

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

---

<div class="post-metadata">

**Author:** ![aikidosama](https://avatars.discourse-cdn.com/v4/letter/a/9d8465/32.png) [@aikidosama](https://discourse.slimframework.com/u/aikidosama)\
**Post date:** [July 11, 2019, 9:32pm UTC](https://discourse.slimframework.com/t/method-getenvironment-addglobal-deprecated/3265/3 "2019-07-11T21:32:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jDolba](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/jdolba/32/815_2.png) [@jDolba](https://discourse.slimframework.com/u/jDolba)\
**Post date:** [July 11, 2019, 10:16pm UTC](https://discourse.slimframework.com/t/method-getenvironment-addglobal-deprecated/3265/4 "2019-07-11T22:16:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aikidosama](https://avatars.discourse-cdn.com/v4/letter/a/9d8465/32.png) [@aikidosama](https://discourse.slimframework.com/u/aikidosama)\
**Post date:** [July 11, 2019, 10:27pm UTC](https://discourse.slimframework.com/t/method-getenvironment-addglobal-deprecated/3265/5 "2019-07-11T22:27:49Z")

</div>

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(  
$container-\>router,  
$container-\>request-\>getUri()  
));  
return $view;  
};

---

<div class="post-metadata">

**Author:** ![jDolba](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/jdolba/32/815_2.png) [@jDolba](https://discourse.slimframework.com/u/jDolba)\
**Post date:** [July 12, 2019, 1:53am UTC](https://discourse.slimframework.com/t/method-getenvironment-addglobal-deprecated/3265/6 "2019-07-12T01:53:34Z")

</div>

> [@aikidosama](#):
>
> **Message:** Call to undefined method Slim\Views\Twig::environment()

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()`

---

<div class="post-metadata">

**Author:** ![aikidosama](https://avatars.discourse-cdn.com/v4/letter/a/9d8465/32.png) [@aikidosama](https://discourse.slimframework.com/u/aikidosama)\
**Post date:** [July 12, 2019, 2:36pm UTC](https://discourse.slimframework.com/t/method-getenvironment-addglobal-deprecated/3265/7 "2019-07-12T14:36:47Z")

</div>

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 [http://help.slimframework.com/discussions/questions/954-twig-getenvironment-function-no-longer-available-using-slim-views](http://help.slimframework.com/discussions/questions/954-twig-getenvironment-function-no-longer-available-using-slim-views)

$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

---

<div class="post-metadata">

**Author:** ![bogdan](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/bogdan/32/1376_2.png) [@bogdan](https://discourse.slimframework.com/u/bogdan)\
**Post date:** [November 25, 2021, 9:57am UTC](https://discourse.slimframework.com/t/method-getenvironment-addglobal-deprecated/3265/8 "2021-11-25T09:57:52Z")

</div>

In Slim 4:

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

becomes:

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