Facebook & CodeIgniter – Definitive Guide

I spent more hours than the expected starting a simple development for a new Facebook application that runs on top of CodeIgniter. First I read A LOT, then I checked for solutions and, of course, I read a little bit more. After several hours reading and coding I knew it wasn’t good at all so I decided to start a new integration method from the scratch using only the code I found as guide. Here’s the result:

0. Environment

Before you get excited just review a few things before spent time reading another useless post :-)

  • PHP Version 5.2.6
  • MySQL 5 (It doesn’t affect Facebook integration at all but it is good to know)
  • HostMonster is my hosting provider (it is not a marketing initiative, just they have a nice environment set up for PHP)
  • Facebook platform 5 – I upload a copy of it to ensure you get the same copy I used.
  • CodeIgniter 1.6.3 – latest version available at the time of this post.

1. Do not hack CodeIgniter

I found a lot of solutions about hacking CodeIgniter by overriding functions or classes. NO, YOU DO NOT HAVE TO DO IT.

2. Install facebook library as plugin

  • Download the facebook platform ZIP
  • Unzip the files wherever you want -outside your Code Igniter application
  • Copy the WHOLE content under /php folder (under means files inside the folder and not with the container folder) to yout ./system/plugins folder inside Code Igniter application -Note: Yes, you have to include the jsonwrapper folder.
  • Rename the facebook.php file to facebook_pi.php

It is done! You already have installed facebook framework as plugin.

3. Autoload facebook plugin [optional]

To avoid the need of including the “load plugin” sentence inside every controller/class you can modify the ./system/config/autoload.php to include facebook plugin as required. It is almost a requirement for Facebook applications. Add to your $autoload['plugin'] array the facebook plugin. It could look like this:

$autoload['plugin'] = array('facebook');

Note: If you skip this step you will have to include the following line every time you need facebook functionality available.

$this->load->plugin('facebook');

4. Setup your application [updated]

Update your configuration file and set the ./system/application/config/config.php and change it as follows:

$config['uri_protocol'] = "PATH_INFO";
$config['base_url'] = "APPLICATION_URL";

Note: If you want to test your application locally, you can set base_url to http://127.0.0.1/path_to_your_app/

5. Extend your Controller class

Because I’m building a 100% facebook application, all my controllers require facebook API available. So I decided to extend my main Controller class as follows – BTW: By the date of this post the CodeIgniter documentation WAS WRONG about how to extend the core classes (CodeIgniter User Guide Version 1.6.3 – Creating Core System Classes). It is not true -i.e. it is false- that you have to extend the CI_ClassName. At least it didn’t work for me at all. Extend the ClassName directly.

Facebook Controller class should be placed under ./system/application/libraries folder.

Filename: XX_Controller.php (where “XX_” is the prefix you set on your ./system/application/config/config.php file – subclass_prefix parameter)

class FB_Controller extends Controller {
 
	// Facebook application key
	var $API_KEY = 'YOUR_API_KEY';
 
	var $facebook;
	var $uid;
 
	/*
	 * Custom Controller constructor.
	 * Adds Facebook support.
	 *
	 */
	function FB_Controller() {
 
		parent::Controller();
 
		// Authentication key
		$secret = 'YOUR_SECRET_KEY'; 
 
		// 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->API_KEY, $secret);
		$this->uid = $this->facebook->require_login();
	}
}

Note: Inside the facebook application I built I always require an authenticated users, so that’s why I do have the require_login() call. However you can also validate if the user already have the application added. You should do this manually inside controller classes because there are a few exceptions where it is needed that user is authenticated but without adding the application.

6. Create your own controllers

Now you can extend your controllers from your Custom Controller. Note: You do not have to add any include or require sentence in your other classes, CodeIgniter will load directly your XX_Controller.php file -if it doesn’t load please check the config.php file and validate the subclass_prefix parameter.

Below you will find an example of a Controller that will validate if the user has the application added in his/her profile.

class Welcome extends XX_Controller {
 
	function Welcome() {
 
		parent::XX_Controller();
 
		// Check if the application has been added by the user
		try {
			if (!$this->facebook->api_client->users_isAppAdded()) {
				$this->facebook->redirect($this->facebook->get_add_url());
				return;
			}
		}
		catch (Exception $x) {
			// Clear cookies for your application and redirect them to a login prompt
			$this->facebook->expire_session();
			$facebook->redirect($this->facebook->get_login_url());
		}
	}
 
	// You should place your Controller's methods below.
 
}

7. Get out and celebrate

CodeIgniter is running as cleaner as you expected and Facebook API was included nicely!

I hope you find this post useful as I said at the beginning.

  PHP Client Library (34,3 KiB, 2.935 hits)