CakePHP URL Shortener Service Tutorial

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

Now that we have our basic layout looking awesome, let’s go ahead and start building our site up to accept urls to smash and shorten! Within the app directory and go into the controller folder. Controllers are the logic, the brains and the masterminds behind the site operations. Once an action is requested the controller jumps into action, pulling data from the database, sending it to the views and then to the browser. Think of the controller as a middleman, a middleman with a heap of power! We’ll create a blank middleman, or task for our middleman, called an action. Create a blank file within the controllers folder and call it shorten_controller.php. Next dump this basic code into it:

1
2
3
4
5
6
7
<?php
	class ShortenController extends AppController {
 
		function index(){
 
		}
?>

This is the barebones of a controller with an action. Firstly we have extended the base class of a controller object of which the geniuses behind CakePHP have created for us. Then we setup a function, called index, which just so happens to be our action! We will build upon this action now, to be our index page, of which will accept a url from a user, validate it, then crunch it, store it in the database and then redirect to another page/action to display the lucky user’s new shortened url.

Let’s get rid of this pesky CakePHP code shall we? In order to do this, we need to tell CakePHP that we have a new default action that we’d like to use for our index/default page. To do this, we’re going to have to route, and route all night long. Open up the routes.php file within the config folder. You will notice this line:

Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));

Awesome. We need to change this to connect our default or ‘/’ root url to the root action we created, which was Shorten::index. To do that, change the previous line to:

Router::connect('/', array('controller' => 'shorten', 'action' => 'index'));

Now check your page again! It should come out a little different. We still get a complaint, but we’re going to handle that now!

3

Let’s take care of this missing table error message now and sort our database out to start accepting and storing urls!

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.