Goal
To make an abstract layer between CakePHP and Facebook, so anyone that knows CakePHP can easily build apps for Facebook without knowing the API.
Installation
CakePlugin::loadAll( array('Facebook' => array('bootstrap' => true)) );
Table:
CREATE TABLE users ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, username VARCHAR(50), uid VARCHAR(50) );
Model:
<?php App::uses('AppModel', 'Model'); class User extends AppModel { } ?>
public $components = array( 'Session', 'Auth' => array( 'loginAction' => array( 'plugin' => 'facebook', 'controller' => 'users', 'action' => 'login' ), 'loginRedirect' => '/', 'logoutRedirect' => '/', 'authenticate' => array( 'all' => array('userModel' => 'User'), 'Facebook.Oauth' ) ) );
public $facebook = array( 'datasource' => 'Facebook.FQL', 'app_url' => 'http://www.yourdomain.com/path/to/cake', // or just http://www.yourdomain.com if it's on the root 'app_id' => '35868871xxxxxx', 'app_secret' => '6059c46e362346xxxxxxxxxxxx' );
You're ready!! Now proceed with the usage section.
Usage
Here are some examples to learn the basis:
// 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);
// 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('FacebookAlbum.name'), 'conditions' => array('FacebookAlbum.owner' => $this->Auth->User('uid')))); $this->set(compact('albums')); // In the view from that action foreach ($albums as $album) { echo $album['FacebookAlbum']['name']." - "; }
Some points to take into account:
- From the CRUD, only R works. This means that only reads.
- For reading, only supports the methods $model->find('all', $options); and $model->find('first', $options);
- You must always include the classname in fields and conditions (e.g. "FacebookAlbum.name", not just "name")
- The models are taken from the FQL tables. The supported ones are:
- FacebookUser (user)
- FacebookAlbum (album)
- FacebookPhoto (photo)
- FacebookPage (page)
- FacebookPageAdmin (page_admin)
- Models are related using CakePHP Model relations.
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: https://github.com/danielauener/cake-social-custom-auth
License
This software is released under the GNU LGPL License.