CakePHP URL Shortener Service Tutorial

Meta: August 26th 2009 // CakePHP // 2,721 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:

1
2
3
4
5
6
7
8
<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!

Tags: , , , , , , , , , , , , , , , , ,

Pages: 1 2 3 4 5 6 7 8

WeDecal.com

Postscript: Leave A Comment // Subscribe (RSS Feed)

2 trackbacks/pingbacks

  1. Pingback: 今週の管理人Bookmark (8/23-8/30) - ElectronicBrain is eating BreakFast on August 31, 2009
  2. Pingback: CakePHP Digest #20 – Nothing Left To Bitch About | PseudoCoder.com on September 24, 2009

Comments About CakePHP URL Shortener Service Tutorial

// 3 comments so far.

  1. Elizabeth Gibbons // August 26th 2009

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

  2. hendrik // August 31st 2009

    great work, thank you!

  3. JP // December 16th 2009

    Awesome tutorial man! Thanks allot

Who Are You?

Your Email Address

Your Website

:D :) :o :eek: :( :lol: :wink: :arrow: :idea: :?: :!: :evil: :p

You can follow any responses to this entry via its RSS comments feed. You may also leave a trackback by clicking this link.