Implementing a Simple Facebook Redirect with CakePHP

November 27, 2007

The previous CakePHP tutorial on Facebook Developer explained how to create a basic Facebook application with the CakePHP framework. However, there are many concepts beyond the basics that must be learned when implementing the Facebook Platform. One such example is how to implement an FBML-based redirect.

Normally, when trying to perform a redirect in PHP, a simple call to the header function does the trick.

header('Location: http://www.yourdomain.com/path/file.php');

Additionally, the CakePHP platform provides a built-in redirect call in its base controller class.

$this->redirect('/controller/action');

In Facebook, redirecting the end user cannot be performed with a standard redirect call as in the above example. Trying to do so will instead redirect the Facebook Platform itself, having it search for FBML output at whatever URL is specified in the redirect call. The end user will still end up at the same page without being redirected, being served the FBML output that your application sends to the Platform.

Advertisement

In order to enable end-user redirects, Facebook provided the fb:redirect FBML tag. By providing the “url” parameter in this tag, the Facebook Platform will parse this FBML, and send a redirect in the HTTP response header to the end user. Because many users include a standard layout (header and footer) for their applications, much extraneous FBML is being rendered with the simple redirect call, causing unnecessary data and delays in total transmission time experienced by the end user.

Fortunately, there is a simple way to resolve this in CakePHP, effectively removing the extraneous data. In the last tutorial, the base application controller (app_controller.php) was defined as the following.

<?php
vendor('facebook/facebook');

class AppController extends Controller {
	var $facebook;

	var $__fbApiKey = 'YOUR_API_KEY';
	var $__fbSecret = 'YOUR_SECRET_KEY';

	function __construct() {
		parent::__construct();

		// Prevent the 'Undefined index: facebook_config' notice from being thrown.
		$GLOBALS['facebook_config']['debug'] = NULL;

		// Create a Facebook client API object.
		$this->facebook = new Facebook($this->__fbApiKey, $this->__fbSecret);
	}
}
?>

This class can be further expanded to include a new redirect function, which will overwrite the base class redirect function. If using the latest stable release of CakePHP (major/minor version 1.1), the following function will do the trick.

function redirect($url, $status = null) {
	// Prevent the action's view from automatically rendering.
	$this->autoRender = false;

	// Return the Facebook redirect FBML.
	echo '<fb:redirect url="'.$url.'"/>';
}

Note that the second parameter, status, is not used. This is because the fb:redirect FBML tag does not make use of such a parameter to be used during a redirect. It is included, however, in order to prevent the parent controller’s redirect method signature from breaking.

Advertisement

For those experimenting with the beta version of CakePHP 1.2, note that a third parameter — $exit — has been added to the base class redirect method signature. The reason for this is to allow the code to exit after calling a redirect, preventing code within the action following the redirect from executing, as well as any post-action event code, such as afterFilter and afterRender. As such, CakePHP 1.2 developers should include this third parameter, as per the below function declaration. (To be fair, CakePHP 1.1 users can technically implement the following function format as well, since the third parameter includes a default value, thereby preventing prior calls to redirect from breaking.)

function redirect($url, $status = null, $exit = false) {
	// Prevent the action's view from automatically rendering.
	$this->autoRender = false;

	// Return the Facebook redirect FBML.
	echo '<fb:redirect url="'.$url.'"/>';

	// Prevent any code following the redirect from being executed.
	if ($exit) {
		exit();
	}
}

Now, within the actions of your CakePHP controllers inherited from this base AppController, redirecting is a piece of cake!

// CakePHP 1.1 stable
$this->redirect('/controller/action');
exit();

// CakePHP 1.2 beta
$this->redirect('/controller/action', null, true);

Share on Facebook      Share This

Comments

6 Responses to “Implementing a Simple Facebook Redirect with CakePHP”

  1. Luis Diego on December 30th, 2007 4:21 pm

    Hi,
    Great tutorial.
    Do you know why the session’s flash doesn’t work on facebook??
    Thanks.

  2. DrLaban on March 22nd, 2008 4:58 pm

    So, I tried this out and got it to work at 99%
    I got everything up and running and am able to browse to my website using a regular URL.
    I’m having a problem with the Canvas URL though. Even though I’ve specified in home.thtml that the redirect is supposed to go to my Canvas URL-page, when I browse to the Canvas URL; http://apps.facebook.com….. Firefox tells me that the page doesn’t redirect properly.

    If I use an inbuilt link within Facebook that directs to my Canvas URL, it reports that the page does not respond.

    I read a bit about your tutorial on redirecting but since I’m not using the redirect function anywhere (as per your tutorial here) I can’t seem to get this up just yet.
    Any hints on what this redirect problem could be about?

  3. Sameen Arshad on May 5th, 2008 2:56 pm

    Hello can anyone tell em that on the canvas page what to write in the callback url and TOS url

  4. emilher on November 21st, 2008 11:31 am

    trato de ingresar desde mi pc al facebook pero no puedo, me aparece redirecting…. y luego dice internet explorer no pudo abrir la pagina alguien me puede ayudar

  5. sandrar on September 10th, 2009 5:35 pm

    Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.

  6. anil on January 25th, 2010 4:08 am

    hi..

    setFlash is not working with redirect…

Got something to say?





Close
E-mail It