Skip to content

Instantly share code, notes, and snippets.

View khepin's full-sized avatar

Sebastien Armand khepin

  • San Francisco, USA
View GitHub Profile
{% extends 'layout.html.twig' %}
{% block content %}
<h1>Welcome to TSUSBOS</h1>
<h2 >The Simple URL Shortener Based On Silex </h2>
<p >To add a new url to be shortened, go to a url formed like this:
<pre >http://shortener.com/goog?url=http://www.google.com </pre>
After this, visiting the url
<pre >http://shortener.com/goog</pre>
will redirect the user to http://www.google.com .
<?php
namespace Khepin;
class UrlService {
//...
public function getAll(){
return $this->urls;
}
<?php
use Khepin\UrlService;
//.... previous code
$app->get('/view/list', function() use($app){
return $app['twig']->render('list.html.twig', array('list' => $app['shortener']->getAll()));
});
{% extends 'layout.html.twig' %}
{% block content %}
<table >
<tr >
<th >Short URL</th>
<th >Full URL</th>
</tr>
{% for url_slug, url in list %}
<tr >
<?php
namespace Khepin;
class UrlShortener {
private $url_list = array();
private $url_file = '';
<?php
require_once __DIR__.'/../vendor/Silex/silex.phar';
/** Bootstraping */
// ... previous code ...
$app['key'] = 'my_key';
/** App definition */
//... previous code ...
{% extends 'layout.html.twig' %}
{% block content %}
<h1 >Congratulations! </h1>
<p >The url {{ url }} can now be shortened via
<a href="/{{ url_slug }}">http://{{ app.request.host }}/{{ url_slug }}</a>
. Feel free to use the service again!
</p>
{% endblock %}
<?php
namespace Khepin;
use Silex\ExtensionInterface;
use Silex\Application;
class ShortenerExtension implements ExtensionInterface {
public function register(Application $app){
$app['shortener'] = $app->share(function() use($app){
return new UrlShortener($app['url_file_name']);
<?php
require_once __DIR__.'/../vendor/Silex/silex.phar';
/** Bootstraping */
$app = new Silex\Application();
$app['key'] = 'my_key';
$app['autoloader']->registerNamespaces(array('Khepin' => __DIR__,));
<?php
//... bootstrap ...
/** App definition */
$app->error(function(Exception $e) use ($app){
if (!in_array($app['request']->server->get('REMOTE_ADDR'), array('127.0.0.1', '::1'))) {
return $app->redirect('/');
}
});