Created
December 9, 2015 00:02
-
-
Save geggleto/a0ff2451fbf9d6e388ef to your computer and use it in GitHub Desktop.
Slim 3 Middleware - Force HTTPS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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); | |
} | |
}); |
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
This code still works well. However, in my case at least I had an error
Therefore, I changed the header of the function to
function ($request, $response, $next)
and it worked.