CodeIgniter + nginx : Facebook application

November 18th, 2008 § 0 comments § permalink

This is a basic tutorial on how to get CodeIgniter facebook application on nginx (with the gotchas). The nginx configuration would be like the following

1
2
3
4
5
6
7
8
9
10
11
12
13
server {
        listen 80;
        server_name blah.com;
        location ~ /index.php/ {
                root           /home/production/blah;
                index  index.html index.htm index.php;
                include        conf/fcgi.conf;
                fastcgi_param  SCRIPT_FILENAME /home/production/fb_apps/quickdate/index.php;
                fastcgi_pass   127.0.0.1:9000;
        }
        access_log      /usr/local/nginx/logs/blah.access_log;
        error_log       /usr/local/nginx/logs/blah.error_log;
    }

The critical line is the fastcgi_params parameter, that changes the whole game. In the code Igniter application you need to add the following file in [app]/system/application/libraries/FB_controller.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
 
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();
        }
}
?>

There are some significant changes in the configuration which is at [app]/system/application/config/config.php

1
2
3
$config['enable_query_strings'] = TRUE;
$config['subclass_prefix'] = 'FB_';
$config['uri_protocol'] = "REQUEST_URI";

Now in the application you should write controllers extending the above defined class , for example I modified the welcome controller.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
 
class Welcome extends FB_Controller {
        function Welcome() {
 
                parent::FB_Controller();
 
                // Check if the application has been added by the user
                try {
                        if (!$this->facebook->api_client->Users_isAppUser()) {
                                $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());
                }
        }
        function index() {
        }
}

Fire this up and see it all running smoothly, this is basically compiled from all kinds of forum over the internet. Thought it will be useful for someone. In case of any issues contact me at me@dipankar.name.