Sponsor
Now Playing
- Alix Perez – I'm Free 2 hours ago
- Alix Perez – Intersections 2 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
This section we’re going to set up our view to work with forms and to get it sending information to our index action, which will then handle the long desired logic. Views are the front end things. They are display files. In saying so they should only contain front end stuff you wish to spit out, in turn, the only logic that you should have here, is display logic (i.e. looping over arrays and outputting their contents). Views are sorted, by convention, in directories, named after their controller. So let’s go ahead and stick to convention (for a change). Make a new directory in your views directory and call it shorten. Inside this new directory we will make a view file for our action, which is called, you got it, index.ctp. Inside it we can dump our code, some of which is CakePHP:
<div id="urlform">
<?php
echo $form->create('shorten');
echo $form->input('url', array('type'=>'text', 'label'=>false, 'class'=>'largeinput', 'error'=>false));
echo $form->submit('Shorten!', array('name'=>'shorten', 'div'=>'rightbutton'));
echo $form->end();
?>
</div>
Save it. Now refresh your page! This is what I see:
If you don’t see that, then please comment below. If you get something similar, you rock. But what is happening? Well we’re using some of CakePHP’s helpers here. A helper is used to alleviate the headaches of mundane tasks, like formatting forms, handling text, dates, pregnancy etc. Here we’re using the form helper, indicated by $form. In more advanced tutorials, you usually would include helpers, which don’t get auto-added like the form helper, in the controller via $helpers = array(‘Javascript’, ‘Time’); for example.
The first line, creates the form tag, of which has an action, method, name etc. That’s automatically handled with the routing etc. The next line sets up an input field, which is linked to the url field of the model. The type would usually be a text field, which CakePHP automatically sets for you, however I prefer a single line, so I have changed the type to text. I don’t want a label or it to display any validation errors, should they arise. I also set the class. Pretty straight forward? Well the next two lines are pretty much the same. End, ends the form!

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