Skip to content

Instantly share code, notes, and snippets.

@caefer
Created January 12, 2012 14:39
Show Gist options
  • Save caefer/1600887 to your computer and use it in GitHub Desktop.
Save caefer/1600887 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bjoerns URL Shortener</title>
<meta name="author" content="Bjoern Wagner">
<!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<!-- Le styles -->
<link rel="stylesheet" href="http://twitter.github.com/bootstrap/1.4.0/bootstrap.min.css">
</head>
<body>
<div class="container">
<?php
require_once __DIR__.'/silex.phar';
use Symfony\Component\HttpFoundation\Request;
$app = new Silex\Application();
// ------ all your code goes below this line ------
$urls = array(
'fabo' => 'http://www.facebook.com',
'twit' => 'http://www.twitter.com',
'plus' => 'http://plus.google.com'
);
function linkList($urls) {
$output = '<ul>';
foreach ($urls as $key => $url) {
$output .= '<li><a href="/' . $key . '" target="_blank">' . $url . '</a></li>' . PHP_EOL;
}
$output .= '</ul>';
$output .= '<br />';
$output .= '<form method="post">';
$output .= 'URL: <input type="text" name="url" /><br />';
$output .= 'Short: <input type="text" name="short" /><br />';
$output .= '<input type="submit" />';
$output .= '</form>';
return $output;
}
$app->get('/', function () use ($urls) {
$output = linkList($urls);
return $output;
});
$app->post('/', function (Request $request) use ($urls) {
$url = $request->get('url');
$short = $request->get('short');
$urls[$short] = $url;
$output = '<p>';
$output .= 'Sie haben die ShortURL "' . $short . '" mit dem Ziel "' . $url . '" angelegt.';
$output .= '</p>';
$output .= linkList($urls);
return $output;
});
$app->get('/{key}', function ($key) use ($urls) {
header('Location: ' . $urls[$key]);
exit();
});
// ------ all your code goes above this line ------
$app->run();
?>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment