Giter VIP home page Giter VIP logo

facebook-plugin-for-cakephp's Introduction

Facebook Plugin for CakePHP

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

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

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:

CakePlugin::loadAll(
    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:

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

Step 4. Enable Auth component in AppController.php and link it to Facebook Authentication. For example:

public $components = array(
    'Session',
    'Auth' => array(
        'loginAction' => array(
            'plugin' => 'facebook',
            'controller' => 'users',
            'action' => 'login'
        ),
        'loginRedirect' => '/',
        'logoutRedirect' => '/',
        'authenticate' => array(
            'all' => array('userModel' => 'User'),
            'Facebook.Oauth'
        )
    )
);

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' => '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:

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('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']." - ";
}

More information

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

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.

facebook-plugin-for-cakephp's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.