Skip to content

Instantly share code, notes, and snippets.

@GromNaN
Created March 15, 2012 23:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GromNaN/2047633 to your computer and use it in GitHub Desktop.
Save GromNaN/2047633 to your computer and use it in GitHub Desktop.
Redirection
{
"name": "jerometamarelle/redirect",
"require": {
"silex/silex": "dev-master"
},
"authors": [
{
"name": "Jérôme Tamarelle",
"email": "jerome@tamarelle.net"
}
]
}
<?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') ?: '&ndash;',
'{{ 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