CakePHP URL Shortener Service Tutorial

CakePHP URL Shortener Service Tutorial

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:

5

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!

Posted by VoiDeT

Categorised under CakePHP
Bookmark the permalink or leave a trackback.

10 Comments

  1. i have never seen such a comprehensive tutortial on this , good on yah, i’ll buy you a paypal beer any day :)

    August 26, 2009 @ 3:07 pm
  2. hendrik

    great work, thank you!

    August 31, 2009 @ 5:39 am
  3. Awesome tutorial man! Thanks allot

    December 16, 2009 @ 4:25 am
  4. 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.

    April 30, 2010 @ 7:43 am
  5. VoiDeT

    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.

    April 30, 2010 @ 9:00 am
  6. The demo file not work http://jotlab.com/tutorials/url/, url not found

    May 23, 2010 @ 3:30 am
  7. 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;
    }

    July 3, 2010 @ 4:11 pm
  8. VoiDeT

    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!

    July 3, 2010 @ 4:14 pm
  9. >>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!

    July 3, 2010 @ 4:19 pm
  10. VoiDeT

    No problem at all! Thanks for your input :)

    July 3, 2010 @ 4:21 pm

5 Trackbacks

  1. [...] CakePHP URL Shortener Service Tutorial • Jotlab [...]

  2. [...] Jotlab has a post on going from clean install to working URL shortener in 8 simple pages. [...]

  3. [...] CakePHP URL Short­ener Ser­vice Tutorial [...]

  4. By 9 Awesome CakePHP Tutorials | DevMoose on June 27, 2010 at 12:02 am

    [...] CakePHP URL Shortener Service Tutorial [...]

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

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

or