webtechnick / CakePHP-Facebook-Plugin

CakePHP Facebook Plugin
http://facebook.webtechnick.com
445 stars 138 forks source link

Facebook Plugin

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.

Changelog

About Plugin

Feature List

Install and Setup

Once installed, if you wish to use any other features other than the share button you'll need to get an api_key and secret for your application.

Usage

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.

Edit your Layout to take advantage of advanced facebook features

  1. In your layout it's highly suggest you replace your <html> tag with <?php echo $this->Facebook->html(); ?> This is required for some of the facebook features to work in IE.
  2. At the bottom of the page include <?php echo $this->Facebook->init(); ?> To load the facebook javascript api to scan your page for fbxml and replace them with various dynamic content.

Example layout

<?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>

Authentication (Facebook Connect/Graph System):

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.

Login/Logout buttons

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

Registration Form

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'
)); ?>

Processing Registration Data.

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..)

CakePHP Auth + Facebook.Connect

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.

Facebook Auth Callbacks

There are three callbacks available to use, each are defined in the controller and are optional to use.

Advanced Helper Feature Examples

<?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(); ?>

Facebook API

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');

Internationalization

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/

Read the Docs

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/