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) 3 hours ago
- Black Sun Empire – Everything 3 hours ago
CakePHP URL Shortener Service Tutorial
August 26, 2009,
3,871 views
If your CakePHP set up is correct you should be seeing a screen similar to:

Congrats! All good! But now let’s make our own style! First things first! We need to pimp our ride out! What we will do now is create a default layout file. A layout file, if you are new to CakePHP, is a file that acts as a parent style page, so you can load other pages or elements within it. For example, a Layout file might include your header, your footer, your contact information, anything that would be displayed on alot of pages that you would rather include in one file, as apposed to duplicating across multiple files.
Navigate into the app folder. This is where all the magic happens. Here are basically all your controls, the previous folder, cake, was the engine. Now checkout the views folder and then go into layouts. Make a blank file called default.ctp. After you have done this, go back to your browser and refresh! Oh no! All the fancy formatting has disappeared. That is because you overwrote cake’s default layout, which was to show you if you had CakePHP properly installed. What you might notice however is text on the screen like:
This is simply debugging information, of which you will see more clearly when we begin to work with the database. Don’t pay too much attention to it at the moment. Open up the default.ctp file you just created and dump this code in and save:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<title>Home.ly | Shorten Like Its Hot</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<?php e($html->css('style.css')); ?>
</head>
<body>
<div id="container">
<div id="page">
<div id="logo"></div>
<div id="contentarea"><?php echo $content_for_layout; ?></div>
<div id="push"></div>
</div>
</div>
<div id="footer">It's Copyright Amigo! <?php echo $html->link('Jotlab.com', 'http://www.jotlab.com'); ?> 2009</div>
</body>
</html>
If you look at your page again it will appear to be quite ugly! But fear not. I have included some images and a css file to go along with it! Where do you put these files? In the webroot folder! The webroot folder acts much like any other folder under an apache server. If you make a folder you can navigate to it with its name in the url. But for example if you try to navigate to your site and app/ you won’t get what you might be expecting! CakePHP has cleverly set up url rewrites to make things both pretty and secure(r).
So with the source zip file provided earlier, upload the style.css file to the css folder in the webroot folder and the images to a new folder called simply images, also in the webroot folder. If you did everything correctly, then you should now be looking at something like:
Shock! The CakePHP stuff is back, but without any of the awesome formatting before! This is because the default controller action is still being called, and being used via the $content_for_layout variable. This variable is used within layouts, it basically says, insert the view code in here. It will come to you, as we’re now going to set up our controller and our view, ready to accept a url!


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 [...]