Skip to content

Instantly share code, notes, and snippets.

@geggleto
Created December 9, 2015 00:02
Show Gist options
  • Save geggleto/a0ff2451fbf9d6e388ef to your computer and use it in GitHub Desktop.
Save geggleto/a0ff2451fbf9d6e388ef to your computer and use it in GitHub Desktop.
Slim 3 Middleware - Force HTTPS
$app->add(function (Request $request, Response $response, $next) {
if ($request->getUri()->getScheme() !== 'https') {
$uri = $request->getUri()->withScheme("https")->withPort(null);
return $response->withRedirect( (string)$uri );
} else {
return $next($request, $response);
}
});
@vladiiancu
Copy link

This code still works well. However, in my case at least I had an error

Argument 1 passed to Closure::{closure}() must be an instance of Request, instance of Slim\Http\Request given

Therefore, I changed the header of the function to function ($request, $response, $next) and it worked.

@tomkyle
Copy link

tomkyle commented Jul 18, 2019

The Request type hint undestands itself under current namespace, if not “use-statemented” otherwise. So you possibly forgot a use statement in your script, like this:

use Psr\Http\Message\ServerRequestInterface as Request;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment