Sponsor
Now Playing
- Alix Perez – I'm Free 3 hours ago
- Alix Perez – Intersections 3 hours ago
- Alix Perez – Forsaken 3 hours ago
- Glen E Ston – Ouroboros (Original Mix) 4 hours ago
- Black Sun Empire – Everything 4 hours ago
CakePHP URL Shortener Service Tutorial
August 26, 2009,
3,871 views
Now that we have our basic layout looking awesome, let’s go ahead and start building our site up to accept urls to smash and shorten! Within the app directory and go into the controller folder. Controllers are the logic, the brains and the masterminds behind the site operations. Once an action is requested the controller jumps into action, pulling data from the database, sending it to the views and then to the browser. Think of the controller as a middleman, a middleman with a heap of power! We’ll create a blank middleman, or task for our middleman, called an action. Create a blank file within the controllers folder and call it shorten_controller.php. Next dump this basic code into it:
<?php
class ShortenController extends AppController {
function index(){
}
?>
This is the barebones of a controller with an action. Firstly we have extended the base class of a controller object of which the geniuses behind CakePHP have created for us. Then we setup a function, called index, which just so happens to be our action! We will build upon this action now, to be our index page, of which will accept a url from a user, validate it, then crunch it, store it in the database and then redirect to another page/action to display the lucky user’s new shortened url.
Let’s get rid of this pesky CakePHP code shall we? In order to do this, we need to tell CakePHP that we have a new default action that we’d like to use for our index/default page. To do this, we’re going to have to route, and route all night long. Open up the routes.php file within the config folder. You will notice this line:
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Awesome. We need to change this to connect our default or ‘/’ root url to the root action we created, which was Shorten::index. To do that, change the previous line to:
Router::connect('/', array('controller' => 'shorten', 'action' => 'index'));
Now check your page again! It should come out a little different. We still get a complaint, but we’re going to handle that now!
Let’s take care of this missing table error message now and sort our database out to start accepting and storing urls!

10 Comments
i have never seen such a comprehensive tutortial on this , good on yah, i’ll buy you a paypal beer any day
great work, thank you!
Awesome tutorial man! Thanks allot
nice post..what about stat a.k.a link tracking? it will be great if u include it.
i bake mine at letsclick.co.cc for learning purpose.
That would be quite easy, just depends how much information you would like to store.
For example you could simply increment a count field in the row for that entry.
Or you could make a completely new table in the database that would log each view/click and the according information, like IP, datetime etc.
The demo file not work http://jotlab.com/tutorials/url/, url not found
Hi, i don like very much the function getFilename. Why dont you try using a function the transform the id of the link into a string with numbers and letters?
pseudocode for add a link;
// find link, if not in database insert new link
// link_shorted= base_convert($link_id, 10, 36);
// return link_shorted
pseudocode for get a encoded link;
// link_id= base_convert($link_code, 36,10);
// search link by id
// redirect
if you want even shorter link you could use this functions;
function any2dec( $num) {
$base=62;
$index = substr( “0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”, 0, $base );
$out = 0;
$len = strlen( $num ) – 1;
for ( $t = 0; $t = 0; $t– ) {
$a = floor( $num / pow( $base, $t ) );
$out = $out . substr( $index, $a, 1 );
$num = $num – ( $a * pow( $base, $t ) );
}
return $out;
}
Exactly right. This is why at the beginning I said it wasn’t the best way to do it. But for the sake of this intermediate tutorial i thought using those baseencodes would be far beyond the scope of the tutorial. I did find similar functions on php.net that would be used to encode/decode ids or unique identifiers. But thanks for your observant comments!
>>Exactly right. This is why at the beginning I said it wasn’t the best way to do it.
upss, i didnt read anything i just read the code, my bad!
No problem at all! Thanks for your input
5 Trackbacks
[...] CakePHP URL Shortener Service Tutorial • Jotlab [...]
[...] Jotlab has a post on going from clean install to working URL shortener in 8 simple pages. [...]
[...] CakePHP URL Shortener Service Tutorial [...]
[...] CakePHP URL Shortener Service Tutorial [...]
[...] 2010 por eugenio85 Etiquetado: acortador, cakephp, PHP, tutorial, url shortener Despues de leer este articulo decidi escribir mi propio tutorial, mientras pensaba en como escribir note que en meneame utilizan [...]