PHP Classes

Slim Simple PHP URL Shortener: Application to create and expand short URLs

Recommend this page to a friend!
  Info   View files Example   Screenshots Screenshots   View files View files (226)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2023-12-02 (5 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 62 All time: 10,432 This week: 79Up
Version License PHP version Categories
slim-url-shortener 1.0GNU General Publi...5.4HTTP, PHP 5, Applications
Description 

Author

This package implements an application to create and expand short URLs.

It provides Web pages to let the users enter a URL, then shows a short version of that URL using the current domain the application is running on.

The application can also redirect the accesses of the short URL to the original URL.

Picture of Pierre-Henry Soria
  Performance   Level  
Name: Pierre-Henry Soria <contact>
Classes: 46 packages by
Country: United Kingdom
Age: 33
All time rank: 37716 in United Kingdom
Week rank: 22 Up1 in United Kingdom Up
Innovation award
Innovation award
Nominee: 17x

Winner: 3x

Example

<?php
/**
 * @author Pierre-Henry Soria <ph7software@gmail.com>
 * @copyright (c) 2013, Pierre-Henry Soria. All Rights Reserved.
 * @link http://github.com/pH-7/Slim-URL-Shortener
 * @license GNU General Public License <http://www.gnu.org/licenses/gpl.html>
 */

namespace PHS;

use
Slim\Slim, Slim\Extras\Views\Twig, PHS\Model\Url;

define('PHS', 1);


require
'constants.php';
require
ROOT_PATH . 'inc/init.inc.php';

putenv('LC_ALL=' . DEFAULT_LANG);
setlocale(LC_ALL, DEFAULT_LANG);

bindtextdomain(LANG_DOMAIN, LANG_PATH);
bind_textdomain_codeset(LANG_DOMAIN, ENCODING);

// Choose domain
textdomain(LANG_DOMAIN);

Slim::registerAutoloader();

// Views
Twig::$twigDirectory = VENDOR_PATH . '/Twig/';

Twig::$twigOptions = array('debug' => DEBUG);

if (
is_writable(TPL_CACHE))
   
Twig::$twigOptions['cache'] = TPL_CACHE;

Twig::$twigExtensions = array(
   
'Twig_Extensions_Slim',
   
'Twig_Extension_Debug',
   
'Twig_Extensions_Extension_I18n'
);


$oApp = new Slim(array(
   
'debug' => DEBUG,
   
'locales.path' => LANG_PATH,
   
'view' => new Twig,
   
'templates.path' => 'views'
));

define('ROOT_URL', $oApp->request()->getUrl() . $oApp->request()->getRootUri());

// Models
\ORM::configure( sprintf('mysql:host=%s;dbname=%s', DB_HOSTNAME, DB_NAME) );
\
ORM::configure('username', DB_USR);
\
ORM::configure('password', DB_PWD);
\
Model::$auto_prefix_models = '\\PHS\Model\\';

$aViewParams = array('encoding' => ENCODING, 'lang' => DEFAULT_LANG, 'url' => ROOT_URL, 'style_name' => STYLE_NAME);

// URLs list
$oApp->get('/', function() use ($oApp)
{
   
$oUrls = \Model::factory('Url');
   
$oAllUrls = $oUrls->order_by_desc('createdDate')->find_many();
   
$iTotalUrls = $oUrls->count();

    global
$aViewParams;
   
$_aViewParams = $aViewParams + array('urls' => $oAllUrls, 'total_urls' => $iTotalUrls); // Update array
   
return $oApp->render('list.twig', $_aViewParams);
});

// Add URL
$oApp->get('/add', function() use ($oApp)
{
    global
$aViewParams;
    return
$oApp->render('add.twig', $aViewParams);
});

// Add URL in submit form
$oApp->post('/add', function() use ($oApp)
{
   
$oUrls = \Model::factory('Url');
   
$sLink = $oApp->request()->post('link');

   
// Check link
   
if (!filter_var($sLink, FILTER_VALIDATE_URL))
    {
       
$oApp->flash('error', gettext('Invalid link!'));
       
$oApp->redirect(ROOT_URL);
    }
    else
    {
       
$oLink = $oUrls->where('link', $sLink);
        if (
$oLink->count() < 1)
        {
           
$oUrl = $oUrls->create();
           
$oUrl->link = $sLink;
           
$oUrl->createdDate = date('Y-m-d H:i:s');
           
$oUrl->ip = $oApp->request()->getIp();
           
$oUrl->save();
        }
       
$oApp->flash('success', sprintf(gettext('The shortened URL is available at: %s'), ROOT_URL . '/' . $oLink->find_many()[0]->id));
       
$oApp->redirect(ROOT_URL); // Go to the list of links
   
}
});

// Contact
$oApp->get('/contact', function() use ($oApp) {
    global
$aViewParams;
    return
$oApp->render('contact.twig', $aViewParams);
});

// Redirect ID towards the URL
$oApp->get('/:id', function($iId) use ($oApp) {
   
$oUrl = \Model::factory('Url')->find_one($iId);
    if (!
$oUrl instanceof Url) {
       
$oApp->notFound();
    } else {
       
$oUrl->nb_access += 1; // Count the number of visit for the link
       
$oUrl->save();
       
$oApp->redirect($oUrl->link);
    }
});

// GO!
$oApp->run();


Details

URL Shortening

Overview

URL Shortener - PHP : |Slim Framework + Twig + Paris & Idiorm| Application to shorten your long URLs. This application also includes multilingual support (i18n).

Server Requirements

PHP 5.4.0 or newer.

Author

Pierre-Henry Soria

Contact Me

ph7software@gmail.com

License

General Public License 3 or later; See also the LICENSE.txt file.


Screenshots  
  • screenshots/sample1.png
  • screenshots/sample2.png
  • screenshots/sample3.png
  Files folder image Files  
File Role Description
Files folder imagedb (1 file)
Files folder imageinc (2 files, 1 directory)
Files folder imagestyles (1 directory)
Files folder imageVendor (3 directories)
Files folder imageviews (4 files)
Accessible without login Plain text file .htaccess Data Auxiliary data
Accessible without login Plain text file constants.php Aux. Auxiliary script
Accessible without login Plain text file index.php Example Example script
Accessible without login Plain text file LICENSE.txt Doc. Documentation
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  db  
File Role Description
  Accessible without login Plain text file schema.sql Data Auxiliary data

  Files folder image Files  /  inc  
File Role Description
Files folder imageModel (1 file)
  Accessible without login Plain text file config.inc.php Aux. Auxiliary script
  Accessible without login Plain text file init.inc.php Aux. Auxiliary script

  Files folder image Files  /  inc  /  Model  
File Role Description
  Plain text file Url.php Class Class source

  Files folder image Files  /  styles  
File Role Description
Files folder imageclassic (1 directory)

  Files folder image Files  /  styles  /  classic  
File Role Description
Files folder imagecss (1 file)

  Files folder image Files  /  styles  /  classic  /  css  
File Role Description
  Accessible without login Plain text file common.css Data Auxiliary data

  Files folder image Files  /  Vendor  
File Role Description
Files folder imageDb (2 files)
Files folder imageSlim (9 files, 4 directories)
Files folder imageTwig (41 files, 11 directories)

  Files folder image Files  /  Vendor  /  Db  
File Role Description
  Plain text file idiorm.php Class Class source
  Plain text file paris.php Class Class source

  Files folder image Files  /  Vendor  /  Slim  
File Role Description
Files folder imageException (2 files)
Files folder imageExtras (1 directory)
Files folder imageHttp (4 files)
Files folder imageMiddleware (5 files)
  Plain text file Environment.php Class Class source
  Accessible without login Plain text file LICENSE Lic. License text
  Plain text file Log.php Class Class source
  Plain text file LogWriter.php Class Class source
  Plain text file Middleware.php Class Class source
  Plain text file Route.php Class Class source
  Plain text file Router.php Class Class source
  Plain text file Slim.php Class Class source
  Plain text file View.php Class Class source

  Files folder image Files  /  Vendor  /  Slim  /  Exception  
File Role Description
  Plain text file Pass.php Class Class source
  Plain text file Stop.php Class Class source

  Files folder image Files  /  Vendor  /  Slim  /  Extras  
File Role Description
Files folder imageViews (1 file)

  Files folder image Files  /  Vendor  /  Slim  /  Extras  /  Views  
File Role Description
  Plain text file Twig.php Class Class source

  Files folder image Files  /  Vendor  /  Slim  /  Http  
File Role Description
  Plain text file Headers.php Class Class source
  Plain text file Request.php Class Class source
  Plain text file Response.php Class Class source
  Plain text file Util.php Class Class source

  Files folder image Files  /  Vendor  /  Slim  /  Middleware  
File Role Description
  Plain text file ContentTypes.php Class Class source
  Plain text file Flash.php Class Class source
  Plain text file MethodOverride.php Class Class source
  Plain text file PrettyExceptions.php Class Class source
  Plain text file SessionCookie.php Class Class source

  Files folder image Files  /  Vendor  /  Twig  
File Role Description
Files folder imageError (3 files)
Files folder imageExtension (8 files, 1 directory)
Files folder imageExtensions (5 files, 4 directories)
Files folder imageFilter (3 files)
Files folder imageFunction (3 files)
Files folder imageLoader (4 files)
Files folder imageNode (23 files, 1 directory)
Files folder imageNodeVisitor (4 files)
Files folder imageSandbox (3 files)
Files folder imageTest (5 files)
Files folder imageTokenParser (17 files)
  Plain text file Autoloader.php Class Class source
  Plain text file Compiler.php Class Class source
  Plain text file CompilerInterface.php Class Class source
  Plain text file Environment.php Class Class source
  Plain text file Error.php Class Class source
  Plain text file ExistsLoaderInterface.php Class Class source
  Plain text file ExpressionParser.php Class Class source
  Plain text file Extension.php Class Class source
  Plain text file ExtensionInterface.php Class Class source
  Plain text file Filter.php Class Class source
  Plain text file FilterCallableInterface.php Class Class source
  Plain text file FilterInterface.php Class Class source
  Plain text file Function.php Class Class source
  Plain text file FunctionCallableInterface.php Class Class source
  Plain text file FunctionInterface.php Class Class source
  Plain text file Lexer.php Class Class source
  Plain text file LexerInterface.php Class Class source
  Accessible without login Plain text file LICENSE Lic. License text
  Plain text file LoaderInterface.php Class Class source
  Plain text file Markup.php Class Class source
  Plain text file Node.php Class Class source
  Plain text file NodeInterface.php Class Class source
  Plain text file NodeOutputInterface.php Class Class source
  Plain text file NodeTraverser.php Class Class source
  Plain text file NodeVisitorInterface.php Class Class source
  Plain text file Parser.php Class Class source
  Plain text file ParserInterface.php Class Class source
  Plain text file SimpleFilter.php Class Class source
  Plain text file SimpleFunction.php Class Class source
  Plain text file SimpleTest.php Class Class source
  Plain text file Template.php Class Class source
  Plain text file TemplateInterface.php Class Class source
  Plain text file Test.php Class Class source
  Plain text file TestCallableInterface.php Class Class source
  Plain text file TestInterface.php Class Class source
  Plain text file Token.php Class Class source
  Plain text file TokenParser.php Class Class source
  Plain text file TokenParserBroker.php Class Class source
  Plain text file TokenParserBrokerInterface.php Class Class source
  Plain text file TokenParserInterface.php Class Class source
  Plain text file TokenStream.php Class Class source

  Files folder image Files  /  Vendor  /  Twig  /  Error  
File Role Description
  Plain text file Loader.php Class Class source
  Plain text file Runtime.php Class Class source
  Plain text file Syntax.php Class Class source

  Files folder image Files  /  Vendor  /  Twig  /  Extension  
File Role Description
Files folder imageExtension (1 file)
  Plain text file Autoloader.php Class Class source
  Plain text file Core.php Class Class source
  Plain text file Debug.php Class Class source
  Plain text file Escaper.php Class Class source
  Plain text file Optimizer.php Class Class source
  Plain text file Sandbox.php Class Class source
  Plain text file Staging.php Class Class source
  Plain text file StringLoader.php Class Class source

  Files folder image Files  /  Vendor  /  Twig  /  Extension  /  Extension  
File Role Description
  Plain text file I18n.php Class Class source

  Files folder image Files  /  Vendor  /  Twig  /  Extensions  
File Role Description
Files folder imageExtension (4 files)
Files folder imageGrammar (11 files)
Files folder imageNode (2 files)
Files folder imageTokenParser (2 files)
  Plain text file Autoloader.php Class Class source
  Plain text file Grammar.php Class Class source
  Plain text file GrammarInterface.php Class Class source
  Plain text file SimpleTokenParser.php Class Class source
  Plain text file Slim.php Class Class source

  Files folder image Files  /  Vendor  /  Twig  /  Extensions  /  Extension  
File Role Description
  Plain text file Debug.php Class Class source
  Plain text file I18n.php Class Class source
  Plain text file Intl.php Class Class source
  Plain text file Text.php Class Class source

  Files folder image Files  /  Vendor  /  Twig  /  Extensions  /  Grammar  
File Role Description
  Plain text file Arguments.php Class Class source
  Plain text file Array.php Class Class source
  Plain text file Body.php Class Class source
  Plain text file Boolean.php Class Class source
  Plain text file Constant.php Class Class source
  Plain text file Expression.php Class Class source
  Plain text file Hash.php Class Class source
  Plain text file Number.php Class Class source
  Plain text file Optional.php Class Class source
  Plain text file Switch.php Class Class source
  Plain text file Tag.php Class Class source

  Files folder image Files  /  Vendor  /  Twig  /  Extensions  /  Node  
File Role Description
  Plain text file Debug.php Class Class source
  Plain text file Trans.php Class Class source

  Files folder image Files  /  Vendor  /  Twig  /  Extensions  /  TokenParser  
File Role Description
  Plain text file Debug.php Class Class source
  Plain text file Trans.php Class Class source

  Files folder image Files  /  Vendor  /  Twig  /  Filter  
File Role Description
  Plain text file Function.php Class Class source
  Plain text file Method.php Class Class source
  Plain text file Node.php Class Class source

  Files folder image Files  /  Vendor  /  Twig  /  Function  
File Role Description
  Plain text file Function.php Class Class source
  Plain text file Method.php Class Class source
  Plain text file Node.php Class Class source

  Files folder image Files  /  Vendor  /  Twig  /  Loader  
File Role Description
  Plain text file Array.php Class Class source
  Plain text file Chain.php Class Class source
  Plain text file Filesystem.php Class Class source
  Plain text file String.php Class Class source

  Files folder image Files  /  Vendor  /  Twig  /  Node  
File Role Description
Files folder imageExpression (17 files, 4 directories)
  Plain text file AutoEscape.php Class Class source
  Plain text file Block.php Class Class source
  Plain text file BlockReference.php Class Class source
  Plain text file Body.php Class Class source
  Plain text file Do.php Class Class source
  Plain text file Embed.php Class Class source
  Plain text file Expression.php Class Class source
  Plain text file Flush.php Class Class source
  Plain text file For.php Class Class source
  Plain text file ForLoop.php Class Class source
  Plain text file If.php Class Class source
  Plain text file Import.php Class Class source
  Plain text file Include.php Class Class source
  Plain text file Macro.php Class Class source
  Plain text file Module.php Class Class source
  Plain text file Print.php Class Class source
  Plain text file Sandbox.php Class Class source
  Plain text file SandboxedModule.php Class Class source
  Plain text file SandboxedPrint.php Class Class source
  Plain text file Set.php Class Class source
  Plain text file SetTemp.php Class Class source
  Plain text file Spaceless.php Class Class source
  Plain text file Text.php Class Class source

  Files folder image Files  /  Vendor  /  Twig  /  Node  /  Expression  
File Role Description
Files folder imageBinary (22 files)
Files folder imageFilter (1 file)
Files folder imageTest (7 files)
Files folder imageUnary (3 files)
  Plain text file Array.php Class Class source
  Plain text file AssignName.php Class Class source
  Plain text file Binary.php Class Class source
  Plain text file BlockReference.php Class Class source
  Plain text file Call.php Class Class source
  Plain text file Conditional.php Class Class source
  Plain text file Constant.php Class Class source
  Plain text file ExtensionReference.php Class Class source
  Plain text file Filter.php Class Class source
  Plain text file Function.php Class Class source
  Plain text file GetAttr.php Class Class source
  Plain text file MethodCall.php Class Class source
  Plain text file Name.php Class Class source
  Plain text file Parent.php Class Class source
  Plain text file TempName.php Class Class source
  Plain text file Test.php Class Class source
  Plain text file Unary.php Class Class source

  Files folder image Files  /  Vendor  /  Twig  /  Node  /  Expression  /  Binary  
File Role Description
  Plain text file Add.php Class Class source
  Plain text file And.php Class Class source
  Plain text file BitwiseAnd.php Class Class source
  Plain text file BitwiseOr.php Class Class source
  Plain text file BitwiseXor.php Class Class source
  Plain text file Concat.php Class Class source
  Plain text file Div.php Class Class source
  Plain text file Equal.php Class Class source
  Plain text file FloorDiv.php Class Class source
  Plain text file Greater.php Class Class source
  Plain text file GreaterEqual.php Class Class source
  Plain text file In.php Class Class source
  Plain text file Less.php Class Class source
  Plain text file LessEqual.php Class Class source
  Plain text file Mod.php Class Class source
  Plain text file Mul.php Class Class source
  Plain text file NotEqual.php Class Class source
  Plain text file NotIn.php Class Class source
  Plain text file Or.php Class Class source
  Plain text file Power.php Class Class source
  Plain text file Range.php Class Class source
  Plain text file Sub.php Class Class source

  Files folder image Files  /  Vendor  /  Twig  /  Node  /  Expression  /  Filter  
File Role Description
  Plain text file Default.php Class Class source

  Files folder image Files  /  Vendor  /  Twig  /  Node  /  Expression  /  Test  
File Role Description
  Plain text file Constant.php Class Class source
  Plain text file Defined.php Class Class source
  Plain text file Divisibleby.php Class Class source
  Plain text file Even.php Class Class source
  Plain text file Null.php Class Class source
  Plain text file Odd.php Class Class source
  Plain text file Sameas.php Class Class source

  Files folder image Files  /  Vendor  /  Twig  /  Node  /  Expression  /  Unary  
File Role Description
  Plain text file Neg.php Class Class source
  Plain text file Not.php Class Class source
  Plain text file Pos.php Class Class source

  Files folder image Files  /  Vendor  /  Twig  /  NodeVisitor  
File Role Description
  Plain text file Escaper.php Class Class source
  Plain text file Optimizer.php Class Class source
  Plain text file SafeAnalysis.php Class Class source
  Plain text file Sandbox.php Class Class source

  Files folder image Files  /  Vendor  /  Twig  /  Sandbox  
File Role Description
  Plain text file SecurityError.php Class Class source
  Plain text file SecurityPolicy.php Class Class source
  Plain text file SecurityPolicyInterface.php Class Class source

  Files folder image Files  /  Vendor  /  Twig  /  Test  
File Role Description
  Plain text file Function.php Class Class source
  Plain text file IntegrationTestCase.php Class Class source
  Plain text file Method.php Class Class source
  Plain text file Node.php Class Class source
  Plain text file NodeTestCase.php Class Class source

  Files folder image Files  /  Vendor  /  Twig  /  TokenParser  
File Role Description
  Plain text file AutoEscape.php Class Class source
  Plain text file Block.php Class Class source
  Plain text file Do.php Class Class source
  Plain text file Embed.php Class Class source
  Plain text file Extends.php Class Class source
  Plain text file Filter.php Class Class source
  Plain text file Flush.php Class Class source
  Plain text file For.php Class Class source
  Plain text file From.php Class Class source
  Plain text file If.php Class Class source
  Plain text file Import.php Class Class source
  Plain text file Include.php Class Class source
  Plain text file Macro.php Class Class source
  Plain text file Sandbox.php Class Class source
  Plain text file Set.php Class Class source
  Plain text file Spaceless.php Class Class source
  Plain text file Use.php Class Class source

  Files folder image Files  /  views  
File Role Description
  Accessible without login Plain text file add.twig Data Auxiliary data
  Accessible without login Plain text file base.twig Data Auxiliary data
  Accessible without login Plain text file contact.twig Data Auxiliary data
  Accessible without login Plain text file list.twig Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 100%
Total:62
This week:0
All time:10,432
This week:79Up