The purpose of the Facebook plugin is to provide a seamless way to connect your cakePHP app to everyone's favorite social networking site -- Facebook. The goal for this plugin is to not only provide extremely useful dynamic features but to also provide a complete interface to the Facebook API.
3.0.1: Added CakePHP 2.0 support
If you do not set a 'model' key, integration with your Auth Model will not happen automatically.
//Example AppController setup
public $components = array('Session',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
),
'authorize' => 'Controller'
),
'Facebook.Connect' => array('model' => 'User')
);
First clone the repository into your app/Plugin/Facebook
directory
git clone git://github.com/webtechnick/CakePHP-Facebook-Plugin.git app/Plugin/Facebook
Load the plugin in your app/Config/bootstrap.php
file:
//app/Config/bootstrap.php
CakePlugin::load('Facebook');
Once you generate an api_key and secret you'll need to create a file app/Config/facebook.php
You can find an example of what you'll need and how it is laid out in /Facebook/Config/facebook.php.example
//app/Config/facebook.php
$config = array(
'Facebook' => array(
'appId' => 'YOUR_APP_ID',
'apiKey' => 'YOUR_API_KEY',
'secret' => 'YOUR_SECRET',
'cookie' => true,
'locale' => 'en_US',
)
);
You can use all or some of the Facebook plugin as you see fit. At the very least you will probably want to use the Facebook Helper
public $helpers = array('Facebook.Facebook');
If all you want to use is the share feature of the Facebook plugin you're all done.
$this->Facebook->share('http://www.example.com/url_to_share'); //(default is the current page).
Nothing else is required for the Facebook share feature. Hoever, to use the more advanced features you'll need to prepare your page a little to handle the fbxml tags.
<html>
tag with <?php echo $this->Facebook->html(); ?>
This is required for some of the facebook features to work in IE.<?php echo $this->Facebook->init(); ?>
To load the facebook javascript api to scan your page for fbxml and replace them with various dynamic content.<?php echo $this->Facebook->html(); ?>
<head>
<title><?php echo $title_for_layout ?></title>
</head>
<body>
<?php echo $content_for_layout; ?>
</body>
<?php echo $this->Facebook->init(); ?>
</html>
Despite the name, the Facebook Connect component takes immediate advantage of the new powerful Facebook Graph API http://developers.facebook.com/docs/api
To use this feature you will first need to update your facebook application with the connect url of your application's url. This is done on the facebook application settings. http://www.facebook.com/developers/apps.php
Now all you need to do is add the Facebook.Connect
component to your app_controller.
public $components = array('Facebook.Connect');
That's it. You're now ready to accept facebook authentication.
Creates a login button: <?php echo $this->Facebook->login() ?>
Create a login button that asks for extended permissions (http://developers.facebook.com/docs/authentication/permissions)
<?php echo $this->Facebook->login(array('perms' => 'email,publish_stream')); ?>
Create a logout button:
<?php echo $this->Facebook->logout() ?>
Each button has multiple options, review the API to see all available options http://docs.webtechnick.com/facebook/classes/FacebookHelper.html
Create a registration form with default fields and width. Default is posting to self.
<?php echo $this->Facebook->registration(); ?>
Create a custom registration form.
<?php echo $this->Facebook->registration(array(
'fields' => 'name,gender,location,email',
'width' => 600,
'redirect-uri' => 'http://www.example.com/process_facebook_registration'
)); ?>
To access the registartion data posted by your registration user, use the convienient ConnectComponent::registrationData() function.
if($user = $this->Connect->registrationData()){
print_r($user);
}
Use the data in $user to finish the registration process on your own (save a new user, find/update the user, etc..)
Facebook.Connect will play nice with a variety of Authentication systems. It has nearly seamless integration with CakePHP AuthComponent.
note Since the CakePHP 2.0 AuthComponent revamp, ConnectComponent doesn't have the introspection available anymore. It is necessary to tell Connect what model you store your users data in for the automagic to work like so:
//Example AppController.php components settup with FacebookConnect
public $components = array('Session',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
),
'authorize' => 'Controller'
),
'Facebook.Connect' => array('model' => 'User')
);
To integrate with CakePHP Auth, you'll need to alter your users table (or whatever table your Auth component uses) and add a new field -> facebook_id
.
ALTER TABLE `users` ADD `facebook_id` BIGINT(20) UNSIGNED NOT NULL
Since you already have an authentication system, the logout step will need to also log out the user from your authentication system.
You do this by passing a redirect to $facebook->logout()
to your system's logout authentication action.
In this case you should set the label or img option if you want the logout button to be displayed.
<?php echo $this->Facebook->logout(array('label' => 'Logout', 'redirect' => array('controller' => 'users', 'action' => 'logout'))); ?>
or
<?php echo $this->Facebook->logout(array('redirect' => array('controller' => 'users', 'action' => 'logout'), 'img' => '/Facebook/img/facebook-logout.png')); ?>
This will log out of the facebook authentication and then redirect to your authentication logout for you to finish the logout.
There are three callbacks available to use, each are defined in the controller and are optional to use.
beforeFacebookSave
handle the user to save into the users table. If returned false, creation is haulted.
//Add an email field to be saved along with creation.
function beforeFacebookSave(){
$this->Connect->authUser['User']['email'] = $this->Connect->user('email');
return true; //Must return true or will not save.
}
beforeFacebookLogin
Handle the user before logging the user into Auth.
function beforeFacebookLogin($user){
//Logic to happen before a facebook login
}
afterFacebookLogin
Handle any needed functionality right after a successful Auth Login
function afterFacebookLogin(){
//Logic to happen after successful facebook login.
$this->redirect('/custom_facebook_redirect');
}
<?php echo $this->Facebook->comments(); ?>
<?php echo $this->Facebook->picture($facebook_id); ?>
<?php echo $this->Facebook->recommendations(); ?>
<?php echo $this->Facebook->like(); ?>
<?php echo $this->Facebook->livestream(); ?>
<?php echo $this->Facebook->activity(); ?>
<?php echo $this->Facebook->friendpile(); ?>
You can access the Facebook Api from anywhere in your app. You'll need to include the Api first
App::uses('FB', 'Facebook.Lib');
Then you can instanciate it or, if you're running PHP 5.3.x you can make static calls on it.
PHP version 5.2.x
$Facebook = new FB();
$Facebook->api('/me');
PHP 5.3.x
FB::api('/me');
You can set the locale of the plugin through the helper declaration or through the config/facebook.php
configuration file (see top of document).
public $helpers = array('Facebook.Facebook' => array('locale' => 'en_US'));
Facebook locales: http://developers.facebook.com/docs/internationalization/
I encourage you to read the documentation and API for this plugin to see all the features and options for each feature. The API is here: http://docs.webtechnick.com/facebook/