Continuously reinventing the wheel in applications development can be time-consuming and ineffective. For this reason, many development frameworks have sprung up with the intention of assisting programmers by providing a jump-start, while at the same time helping to keep code organized and easily manageable. Now, with the help of the instructions below, you can learn how to create a Facebook Application with the relatively simple CakePHP framework.
Step 1. The first step is to create a Facebook application. In setting up, only a few of the settings are vital for getting started.
* Include an application name of your preference.
* Within the ?Optional Fields? section, provide the callback URL where you intend to host the application. The callback URL should point to the root path of your CakePHP installation.
* Enter a Canvas Page URL that has not already been taken, and make sure FBML is selected.
* If you do not want friends or other random users installing your application yet, make sure to check off the Developer Mode box.
* Lastly, set the Side Nav URL to the same value as the Canvas Page URL.
Step 2. Now that your application is set up, you'll need to download and unzip the latest stable version of CakePHP.
Step 3. Create a new folder within your CakePHP application under ?/app/vendor/facebook?. Download the latest version of the Facebook Platform API, and unzip the contents of the ?client? folder into the new ?facebook? folder created a moment ago. (PHP4 users should unzip the contents of the ?php4client? folder instead.) You should now have the directory contents as follows.
Step 4. You'll now need to modify the AppController base class such that all your inherited controllers utilize the Facebook API. To start, copy app_controller.php from ?/cake? to ?/app?. Next, open the file up in your preferred text editor, and change its contents to match the following. (Make sure you change the values for the Facebook API key and secret in the process.)
view plaincopy to clipboardprint?
1.
2. vendor('facebook/facebook');
3.
4. class AppController extends Controller {
5. var $facebook;
6.
7. var $__fbApiKey = 'YOUR_API_KEY';
8. var $__fbSecret = 'YOUR_SECRET_KEY';
9.
10. function __construct() {
11. parent::__construct();
12.
13. // Prevent the 'Undefined index: facebook_config' notice from being thrown.
14. $GLOBALS['facebook_config']['debug'] = NULL;
15.
16. // Create a Facebook client API object.
17. $this->facebook = new Facebook($this->__fbApiKey, $this->__fbSecret);
18. }
19. }
20. ?>
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);
}
}
?>
Step 5. Create a basic controller class that inherits the AppController defined above. Here we'll perform basic Facebook calls such as logging in. Additionally, an example view named ?index? is included, representing the index page of the things controller.
view plaincopy to clipboardprint?
1.
2. class ThingsController extends AppController {
3. var $user;
4.
5. /**
6. * Name: beforeFilter
7. * Desc: Performs necessary steps and function calls prior to executing
Step 6. Create a placeholder model object under ?/app/model? named thing.php. Again, place the following contents into the new file.
view plaincopy to clipboardprint?
1.
2. class Thing extends AppModel {
3. var $name = 'Thing';
4. var $useTable = false;
5. }
6. ?>
class Thing extends AppModel {
var $name = 'Thing';
var $useTable = false;
}
?>
Step 7. In order to ensure consistency between pages, you'll want to create a default layout. This is a place to include header and footer FBML. Create a new document in your text editor named ?/app/views/layouts/default.thtml?, and insert some code such as the following. The vital part that must be included is the echo call to print the $content_for_layout variable.
view plaincopy to clipboardprint?
1.
2.
3.
6.
7.
8. My Things
9. Browse Things
10. Search Things
11. Add Things
12.
13.
14.
.container { padding:10px; }
My Things
Browse Things
Search Things
Add Things
Step 8. Finally, you need to create a file that represents the layout of the index view defined as a function of ThingsController from step 5. Create a new file named ?/app/views/things/index.thtml?, and insert the below contents. Note the use of the $friends variable, which was passed from the index function via a call to the controller's set function.
view plaincopy to clipboardprint?
1. My Things
2. My Friends:
3.
4.
5.
6.
7.
My Things
My Friends:
Step 9. The last step is to upload your cake application to your server (making sure to match the callback URL path set for your application). You can now access the page via: http://apps.facebook.com/YOUR-APP-PATH/things.
Follow-up Suggestions
You may be wondering what happens when visitors arrive at your application via http://apps.facebook.com/YOUR-APP-PATH/ instead of http://apps.facebook.com/YOUR-APP-PATH/things. With the way things are currently set up, visitors will see a default CakePHP output explaining how to overwrite that page.
To fix this, you'll need to have YOUR-APP-PATH redirect to YOUR-APP-PATH/things. Normally this could be done with a simple change to /app/config/routes.php. However, since routes in CakePHP are used for redirects, and because we can't perform standard redirects within Facebook apps, a simple workaround is necessary.
Create the file ?/app/views/pages/home.thtml?, and add an fb:redirect tag, as per the following example.
view plaincopy to clipboardprint?
1.
Now, when your application's root remote path is called, it will retrieve the contents of the home.thtml file (within the default layout you created). The redirect will be interpreted by Facebook, and the end user will be sent to the things index page as we expected.
If using a database, you'll of course want to set up the connection for CakePHP to utilize it. Under ?/app/config?, rename ?database.php.default? to ?database.php?. Edit the file, and update the host, login, password, and database values for the $default array variable.
Need To advertise your Facebook Application? Then the best way to do this would be using a tool like Facebotpro.com . Facebot Pro is a Facebook Friend Adder that is designed for getting your word out on Facebook. Works perfect for promoting Applications!
When using Facebotpro you will want to ofcourse in put your account information and log in.
Write up a great email describing your application and giving the link location where it is at on Facebook.com for users to install to their profiles. List some great features and benefits ofcourse in the message but do not give to much info, keep them curious and wanting to know more so they check out what you have to offer.
2. Gather the Id's to the users you would want to send friend requests to, You might want to target your search, Example: If you have a application that is meant for girls, then gather all girls id's in the search. Better results this way.
3. That's it, Send out your message and see how many users you have using your application! You can use Facebot Pro for anything other then promoting applications, use it to promote your music, or business.