# Slim 4 Set Environment

**URL:** <https://discourse.slimframework.com/t/slim-4-set-environment/3920>\
**Category:** Questions\
**Created:** [March 2, 2020, 12:11pm UTC](https://discourse.slimframework.com/t/slim-4-set-environment/3920 "2020-03-02T12:11:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![elvisreis](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/elvisreis/32/1298_2.png) [@elvisreis](https://discourse.slimframework.com/u/elvisreis)\
**Post date:** [March 2, 2020, 12:11pm UTC](https://discourse.slimframework.com/t/slim-4-set-environment/3920/1 "2020-03-02T12:11:25Z")

</div>

In slim 3 I managed to call a route via cmd php cron.php I recreated an `Environment::mock` flame to create a route for the file via cli.

On the slim 4 how do I set this variable, because on the slim 3 all settings were via the container

reference slim 3 [https://www.slimframework.com/docs/v3/cookbook/environment.html](https://www.slimframework.com/docs/v3/cookbook/environment.html)

```auto
$env = \Slim\Psr7\Environment::mock([
        'REQUEST_URI' => '/' . pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_FILENAME),
        'HTTP_HOST' => getenv("HTTP_HOST"),
        'HTTPS' => getenv("HTTPS")
    ]);

$container['environment'] = $env;

```

---

<div class="post-metadata">

**Author:** ![elvisreis](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/elvisreis/32/1298_2.png) [@elvisreis](https://discourse.slimframework.com/u/elvisreis)\
**Post date:** [May 5, 2020, 12:00pm UTC](https://discourse.slimframework.com/t/slim-4-set-environment/3920/2 "2020-05-05T12:00:41Z")

</div>

Help, not working

$container-\>set(‘environment’, function (ContainerInterface c) { return \Slim\Psr7\Environment::mock(['REQUEST\_URI' =\> '/' . pathinfo(\_SERVER[‘SCRIPT\_NAME’], PATHINFO\_FILENAME),  
‘HTTP\_HOST’ =\> getenv(“HTTP\_HOST”),  
‘HTTPS’ =\> getenv(“HTTPS”)  
]);  
});

---

<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:** [May 5, 2020, 3:01pm UTC](https://discourse.slimframework.com/t/slim-4-set-environment/3920/3 "2020-05-05T15:01:17Z")

</div>

You could try to add a middlware before (after) the routing middleware that changes the request host, https and uri.

---

<div class="post-metadata">

**Author:** ![elvisreis](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/elvisreis/32/1298_2.png) [@elvisreis](https://discourse.slimframework.com/u/elvisreis)\
**Post date:** [May 6, 2020, 3:05pm UTC](https://discourse.slimframework.com/t/slim-4-set-environment/3920/4 "2020-05-06T15:05:02Z")

</div>

Thanks

```auto
<?php

namespace App\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use Slim\Psr7\Environment;
use Slim\Psr7\Uri;

class CronMiddleware implements MiddlewareInterface
{
    public function process(Request $request, RequestHandler $handler): ResponseInterface
    {
        if (php_sapi_name() === 'cli') {
            $data = Environment::mock([
                'REQUEST_URI' => '/' . pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_FILENAME),
                'HTTP_HOST' => getenv("HTTP_HOST"),
                'HTTPS' => getenv("HTTPS")
            ]);

            $uri = new Uri($data["REQUEST_SCHEME"], $data["HTTP_HOST"], $data["SERVER_PORT"], $data["REQUEST_URI"]);
            $request = $request->withUri($uri);
        }

        return $handler->handle($request);
    }
}

```
