Created
March 15, 2012 23:26
-
-
Save GromNaN/2047633 to your computer and use it in GitHub Desktop.
Redirection
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
{ | |
"name": "jerometamarelle/redirect", | |
"require": { | |
"silex/silex": "dev-master" | |
}, | |
"authors": [ | |
{ | |
"name": "Jérôme Tamarelle", | |
"email": "jerome@tamarelle.net" | |
} | |
] | |
} |
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
<?php | |
require __DIR__.'/vendor/.composer/autoload.php'; | |
use Silex\Application; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\HttpFoundation\RedirectResponse; | |
$app = new Application(); | |
$app->get('/', function() { | |
$html = <<<HTML | |
<html> | |
<body> | |
<h1>Compare HTTP redirection codes by yourself</h1> | |
<form action="redirect" method="POST"> | |
<h3>POST</h3> | |
<input type="text" name="value" value="original" /> | |
<input type="submit" value="301" name="code" /> | |
<input type="submit" value="302" name="code" /> | |
<input type="submit" value="303" name="code" /> | |
<input type="submit" value="307" name="code" /> | |
</form> | |
<form action="redirect" method="GET"> | |
<h3>GET</h3> | |
<input type="text" name="value" value="original" /> | |
<input type="submit" value="301" name="code" /> | |
<input type="submit" value="302" name="code" /> | |
<input type="submit" value="303" name="code" /> | |
<input type="submit" value="307" name="code" /> | |
</form> | |
<p>Source code on <a href="https://gist.github.com/2047633" title="Gist 2047633">GitHub</a>, by <a href="http://twitter.com/GromNaN">GromNaN</a></p> | |
</body> | |
</html> | |
HTML; | |
return new Response($html); | |
}); | |
$app->match('/redirect', function () use ($app) { | |
$code = $app['request']->get('code'); | |
$url = 'end?x=' . microtime(true); | |
$response = new RedirectResponse($url, $code); | |
$response->setCache(array('max_age' => 3600)); | |
return $response; | |
}); | |
$app->match('/end', function () use ($app) { | |
$html = <<<HTML | |
<html> | |
<body> | |
<ul> | |
<li>HTTP Method: {{ method }}</li> | |
<li>POST value: {{ value }}</li> | |
<li>Redirection page cached: {{ cache }} (invalid the first time)</li> | |
</ul> | |
<p><a href="./">Return to the previous page</a>, by <a href="http://twitter.com/GromNaN">GromNaN</a></p> | |
<body> | |
</html> | |
HTML; | |
return new Response(strtr($html, array( | |
'{{ method }}' => $app['request']->getMethod(), | |
'{{ value }}' => $app['request']->request->get('value') ?: '–', | |
'{{ cache }}' => ((float) $app['request']->query->get('x') < microtime(true) - 0.2) ? 'Yes' : 'No', | |
))); | |
}); | |
$app->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment