View on GitHub

Facebook Plugin for CakePHP

DataSource and oAuth for CakePHP 2.x based on Facebook API

Download this project as a .zip file Download this project as a tar.gz file


To make an abstract layer between CakePHP and Facebook, so anyone that knows CakePHP can easily build apps for Facebook without knowing the API.


Step 1. Download, unzip and rename the folder to Facebook and move it to the Plugin's folder of your CakePHP project.
Step 2. In Config/bootstrap.php, enable the plugin and it's bootstrap, by adding a line like this:
	array('Facebook' => array('bootstrap' => true))
Step 3. You must have a table for Users and a model, with at least one field for "uid". If you don't have one, something like this is fine:


    username VARCHAR(50),
    uid VARCHAR(50)


App::uses('AppModel', 'Model');

class User extends AppModel {

Step 4. Enable Auth component in AppController.php and link it to Facebook Authentication. For example:
public $components = array(
	'Auth' => array(
		'loginAction' => array(
			'plugin' => 'facebook',
			'controller' => 'users',
			'action' => 'login'
		'loginRedirect' => '/',
		'logoutRedirect' => '/',
		'authenticate' => array(
			'all' => array('userModel' => 'User'),
Step 5. In Config/database.php, enter the AppKey and AppSecret from your Facebook App, your app complete url, and your login action:
public $facebook = array(
	'datasource' => 'Facebook.FQL',
	'app_url' => '', // or just if it's on the root
	'app_id' => '35868871xxxxxx',
	'app_secret' => '6059c46e362346xxxxxxxxxxxx'

You're ready!! Now proceed with the usage section.


Here are some examples to learn the basis:

Example 1. How to natively login to CakePHP Auth using Facebook:

// In the Controller include FacebookHelper
public $helpers = array('Facebook.Facebook');

// In the View (probably in Users/login.ctp) print the login button
echo $this->Facebook->loginButton();

// In the View print the logout button
echo $this->Html->link('Logout', array('plugin' => 'facebook', 'controller' => 'users', 'action' => 'logout'));

By default, the above example will print a raw link with the label "Login". If you want to customize it, you can easily do it by passing some params:

// The text of the link
$label = "Facebook Login!";

// The same options as HtmlHelper::link()
$options = array(
	'class' => 'btn_login',
	'id' => 'facebook'

// The permissions we need from the user
$permissions = array('email','user_photos');

echo $this->loginButton($label, $options, $permissions);

Example 2. Get all the albums from the user:

// In the Controller include the FacebookAlbum model
public $uses = array('Facebook.FacebookAlbum');
// In the same Controller, in an action
$albums = $this->FacebookAlbum->find('all', array('fields' => array(''), 'conditions' => array('FacebookAlbum.owner' => $this->Auth->User('uid'))));

// In the view from that action
foreach ($albums as $album) {
	echo $album['FacebookAlbum']['name']." - ";
More information

Some points to take into account:

How to Contribute

You can contribute by working directly on the core or expanding the supported models.

If you want to contribute please get in touch through the support section, by twitter or github. Also, you can fix or submit new issues on github.

Authors and Contributors

Copyright (c) 2012 - Mariano Finochietto // twitter: @finomdq // github: @marianofino.

The Auth component is based on the danielauener's "FacebookAuthenticate". You can find more information here:


This software is released under the GNU LGPL License.

Comments & Support

comments powered by Disqus