Skip to content

Instantly share code, notes, and snippets.

@pentagonal
Last active June 24, 2016 13:54
Show Gist options
  • Save pentagonal/9821659837c7331afe539866bdc5d0c3 to your computer and use it in GitHub Desktop.
Save pentagonal/9821659837c7331afe539866bdc5d0c3 to your computer and use it in GitHub Desktop.
Fix Scheme Problem & Rewrite Mode on Slim Frame Work 3
<?php
/**
* This only example usage of using slim as application
* By default Slim 3 only detect if environemnt https set not 'off'
* but on some case eg : cloudflare Flexible SSL / FULL SSL will be hide site behind
* the CloudFlare Secure Protocol.
* You could change anything namespace etc. to working with Slim 3 App
* @see {https://support.cloudflare.com/hc/en-us/articles/203487280--How-do-I-fix-the-infinite-redirect-loop-error-after-enabling-Flexible-SSL-with-WordPress-}
* on some case cloud flare use Environment (HTTP_X_FORWARDED_PROTO)
* to hide real site and serve on their secure protocol
*/
namespace MyApp;
use Slim\App;
use Slim\Http\Uri;
use Slim\Http\Environment;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
class MyApp
{
/**
* @var \Slim\App
*/
protected $slim;
/**
* MyApp constructor
*/
public function __construct()
{
// call Fix Protocol Environment
$server_env = $this->fixHTTPSEnvironment();
$this->slim = new App(
[
// even though this is a optional,
// but better to set this (my opinion)
'environment' => Environment::mock($server_env),
]
);
/**
* Optional , if you have mod rewrite enable
* and prevent duplicate URL, you could call
* @method MyApp::registerMiddleWareFixRewrite()
*/
$this->registerMiddleWareFixRewrite();
}
/**
* Detecting & Fix Environment on some cases
* Default Environment uses $_SERVER to attach
* just to fix https
* @return array
*/
protected function fixHTTPSEnvironment()
{
if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off'
// hide behind proxy / maybe cloudflare cdn
|| isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https'
|| !empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off'
) {
// detect if non standard protocol
if ($_SERVER['SERVER_PORT'] == 80
&& (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])
|| isset($_SERVER['HTTP_FRONT_END_HTTPS'])
)
) {
// add original standard port
$_SERVER['SERVER_PORT_ORIGINAL'] = 80;
$_SERVER['SERVER_PORT'] = 443;
}
// fixing HTTPS Environment
$_SERVER['HTTPS'] = 'on';
}
return $_SERVER;
}
/**
* Register Additional Middleware to make sure getting better on
* mod_rewrite, prevent multiple /duplicate URL
*
* @return $this
*/
protected function registerMiddleWareFixRewrite()
{
$this->slim->add(function (
ServerRequestInterface $requestInterface,
ResponseInterface $responseInterface,
$next
) {
/**
* clone of environment toprevent override of environment setted
*
* @var Environment
*/
$env = clone $this->environment;
// use parent directory of script name
$env['SCRIPT_NAME'] = dirname($env['SCRIPT_NAME']);
$requestInterface =
$this->request = // overwrite request for all resource
$requestInterface->withUri(Uri::createFromEnvironment($env));
return $next($requestInterface, $responseInterface);
});
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment