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
$env = \Slim\Psr7\Environment::mock([
'REQUEST_URI' => '/' . pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_FILENAME),
'HTTP_HOST' => getenv("HTTP_HOST"),
'HTTPS' => getenv("HTTPS")
]);
$container['environment'] = $env;
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”)
]);
});
odan
3
You could try to add a middlware before (after) the routing middleware that changes the request host, https and uri.
Thanks
<?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);
}
}