CakePHP URL Shortener Service Tutorial

Meta: August 26th 2009 // CakePHP // 2,738 views

Make a new view file called short.ctp which should be placed in the shorten directory within views. Next add the following code to get our view looking awesome:

1
<div id="bigurl"><?php echo $html->link('http://home.ly/'.$shorturl, array('controller'=>'shorten', 'action'=>'redirectUrl', 'shortcode'=>$shorturl)); ?></div>

This just shows our URL as a link, which we used CakePHP’s html helper to produce! The first part of the link is what is displayed, we faked it to be home.ly, we don’t own the domain, but for presentation sake, we included it, when it really should be: http://jotlab.com/tutorial/url/……hardly short! The second part links through a route to a action we will make a bit later on. So I will explain this url a bit more then. Easy!

So now we have a semi functional website, we can add urls, we can see the url that has been shortened. What you might of noticed however, is that the urls you enter don’t have to be urls to be shortened! This isn’t going to work! So we’ll need to put in some validation to make sure that the url submitted, is in fact, a url. Like I said before, our models need to be big fatty boom batty models, it just works better this way. Well guess what? Validation goes in models too! So let’s add it in already! Open up your model again, and add:

6
7
8
var $validate = array(
			'url' => array('rule' => array('url', true))
		);

What? That’s it? Yep! This sets up the validation. On the left is the field that is being tested, the url input field from the form. Then we apply a rule on it, which just happens to be a url rule, which CakePHP has pre-prepared for you! What is this true thing? That is a little feature that the developers have built in, that for the url validation, makes sure there is a protocol added with the url. So if you enter in me.com it won’t validate, but if you enter in http://me.com it will! You lucky thing you. So go ahead, try enter a url, try test it out. It works!

So we have an application that adds, displays, validates and washes the dishes. “But there’s no HBO!!!” you cry, the urls don’t redirect when you go to them! We’ll let’s go set this up. In order to do this, we need to set up another action, and also a route to catch all and route to the action which redirects! First let’s make the action (add this to your shorten_controller.php):

21
22
23
24
25
26
27
28
function redirectUrl($shortcode){
	$fullurl = $this->Shorten->getFullUrl($shortcode);
	if(!empty($fullurl)){
		$this->redirect($fullurl);
	} else {
		$this->redirect(array('controller'=>'shorten', 'action'=>'index'));
	}
}

What this does is sets up another action, but it receives something, something that our route will pass to it. The action also talks to our model again, to get the full url of the particular shortened url. The action then checks if the model spat back a url, basically whether it existed or not. If the url is present, then we redirect the user to it, if it is not, then with our route, we redirect our user back to the index page to insert a url. The model function is quite straight forward:

44
45
46
47
function getFullUrl($shortcode){
	$fullurl = $this->field('url', array('shortcode'=>$shortcode));
	return $fullurl;
}

This just grabs the record’s url field which is associated to the unique shortcode, which was randomly generated before. Both the controller and model functions however depend on one thing as you have seen, the $shortcode variable. Where does this come from? Well it comes from the URL itself! Our route picks this up and passes it to our controller, which then uses it to pass to our model. Where is this so called route you ask? Well open up your routes.php file and under the previous route add:

35
Router::connect('/:shortcode', array('controller' => 'shorten', 'action' => 'redirectUrl'), array('pass'=>array('shortcode')));

You should only have two routes, these two, and in this order:

34
35
Router::connect('/', array('controller' => 'shorten', 'action' => 'index'));
Router::connect('/:shortcode', array('controller' => 'shorten', 'action' => 'redirectUrl'), array('pass'=>array('shortcode')));

As you can see, we really only have two pages. Routes operate in order of precedence, if the page is simply / then it will go to the index, but anything else, anything after the / if present will route to the redirectUrl action! The :shortcode captures the string within the url, and then we tell it to pass it to the controller via array(‘pass’=>array(’shortcode’)). So with these three things added: your route, your action and your model function, your site is now 100% operational! Go try adding a url, then navigating to the link or clicking on the url generated after you submit a url! Yayyyy, you got redirected!

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.