This file contains hidden or 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
| {% 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 . |
This file contains hidden or 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 | |
| namespace Khepin; | |
| class UrlService { | |
| //... | |
| public function getAll(){ | |
| return $this->urls; | |
| } |
This file contains hidden or 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 | |
| use Khepin\UrlService; | |
| //.... previous code | |
| $app->get('/view/list', function() use($app){ | |
| return $app['twig']->render('list.html.twig', array('list' => $app['shortener']->getAll())); | |
| }); |
This file contains hidden or 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
| {% extends 'layout.html.twig' %} | |
| {% block content %} | |
| <table > | |
| <tr > | |
| <th >Short URL</th> | |
| <th >Full URL</th> | |
| </tr> | |
| {% for url_slug, url in list %} | |
| <tr > |
This file contains hidden or 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 | |
| namespace Khepin; | |
| class UrlShortener { | |
| private $url_list = array(); | |
| private $url_file = ''; |
This file contains hidden or 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_once __DIR__.'/../vendor/Silex/silex.phar'; | |
| /** Bootstraping */ | |
| // ... previous code ... | |
| $app['key'] = 'my_key'; | |
| /** App definition */ | |
| //... previous code ... |
This file contains hidden or 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
| {% 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 %} |
This file contains hidden or 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 | |
| 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']); |
This file contains hidden or 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_once __DIR__.'/../vendor/Silex/silex.phar'; | |
| /** Bootstraping */ | |
| $app = new Silex\Application(); | |
| $app['key'] = 'my_key'; | |
| $app['autoloader']->registerNamespaces(array('Khepin' => __DIR__,)); |
This file contains hidden or 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 | |
| //... 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('/'); | |
| } | |
| }); |