Extending CakePHP Facebook plugin

Currently I am working on an application which require Facebook authentication for it’s users. As you may guess its written in CakePHP, and I’ve searched on Internet for a plugin which integrate Facebook in CakePHP application.

There is one very good CakePHP Facebook plugin written from Nick Baker, and I give a try. It’s really good plugin which provides everything for integration of Facebook (even the Facebook API) in your site. Full list example of functionality can be seen here.

Although, I’ve notice that the site is getting slow when the user is connected on the Facebook. Digging in the code I’ve noticed that the initialize() function which is responsible for the initialisation of the plugin request every time the data for the logged user from the Facebook, and this of course slows down the Application itself, because initialize() is called on every page refresh in every site.

What I’ve did – a little optimisation of the code. Here is the explanation how the improved core works:

  1. on init (initialize() function) it just assign the FB instance to the component and getting the Facebook session. This doesn’t require FB call.
  2. There is an extra parameter which could be provided to the plugin’s settings – noAuth. If this parameter is set the plugin wont attempt to login using the Auth component in the site. By default this parameter isn’t set, but it’s more convenient to have it, because in some cases like mine, I don’t use user Auth on the site except the Facebook’s. Here is a sample how to set noAuth for the plugin:
    class AppController extends Controller {
    $components = array('Facebook.Connect'=>array('noAuth'=>true));
  3. Finally in your AppController::beforeFilter() you can use following in order to get the logged user’s details:
    class AppController extends Controller {
    function beforeFilter(){
    $fb = $this->Connect->user();

    This call has been already cached and you don’t need to worry about extra calls to Facebook site.

Another think which I’ve seen in the comments for the Nick’s article is the request to disconnect (unsubscribe) from the application without logging out of the Facebook. That’s why I’ve added a function disconnect() which doing exactly this.


    echo $this->Facebook->disconnect(array(
                     'label'=>__('exit', true),
                     'confirm'=>__('Are you sure?', true)

Possible options which could be provided in the function:

  • label – string text for the link
  • redirect – action to which user will be redirected after the logged out. If it’s not set the page will be reload.
  • confirm – javascript confirm message (for example “Are you sure?”) before user disconnected from the application.

I’ve added the confirm option in the logout as well (with the same functionality), as well as there is a new option parameter custom which will force the link creation instead or fbml logout link, because in the original implementation custom link is used only if you pass redirect action, but in some cases it need only to reload the page without any redirection and it’s convenient to have a custom link.

And finally where is the extended plugin? Of course in guthub. CakePHP-Facebook-Plugin

Update: The changes are already accepted and included in the original author of the plugin and you can found them at Plugin’s home page

11 thoughts on “Extending CakePHP Facebook plugin

  1. nick

    Thank you very much for taking the time to contribute back to the plugin. I’ve pulled in your changes and updated the version number to 2.1.1.

    There were just two small bugs in your pull request as well as many broken tests due to the optimizations. I’ve fixed both bugs and the plugin is now strict compliant. The changes are now live and active on http://facebook.webtechnick.com

    Thanks again for your contribution!

  2. Nik Chankov Post author

    Probably it’s good to put a link “disconnect” along with the Signout link in the corner of the page, so users will check this functionality as well 🙂

  3. Jamie Chong

    You guys are awesome. I’m using this plugin for a website I’m releasing next month. So far it works great! Thanks for all the hard work.

  4. Javier

    Hi Nik!
    First congratulations for the great job with this plugin.
    I need build this plugin in a web in cakephp but when I click on login button and I enter my facebook account, the website refresh it self all the time…it’s like it want auth in but it doesn’t manage.
    Maybe it’s happend because I have in my model “username” but the password row is called “crypted_password” and save the pass with md5.
    Also I’m not working whit auth component in appcontroller even if with user_controller yes.

    Do you have any idea?

    thanx in advance

  5. Nik Chankov Post author

    Hi Javier,

    Actually the plugin isn’t mine. The original author is Nick Baker and my involvement was just to improve few parts of the code. Actually I haven’t changed much the Auth part of it. So, basically it’s better to check Nick’s blog and ask there this question.

  6. Ted

    I used Facebook Plugins V3.x and I followed the your tutorials. other facebook function did not work properly like like(), login(), share, ect…

    I have a problem on Facebook needs Curl PHP when I user var $components = array(‘Facebook.Connect’);

    displayed after refreshed the page.
    “Uncaught exception ‘Exception’ with message ‘Facebook needs the CURL PHP extension.’ in C:\xampp\htdocs\jergens_facebook\app\plugins\facebook\vendors\facebook\src\facebook.php:4 Stack trace: #0 C:\xampp\htdocs\jergens_facebook\cake\libs\configure.php(1067): require() #1 C:\xampp\htdocs\jergens_facebook\cake\libs\configure.php(1043): App->__load(‘C:\xampp\htdocs…’) #2 C:\xampp\htdocs\jergens_facebook\cake\libs\configure.php(962): App->__find(‘facebook/src/fa…’, true) #3 C:\xampp\htdocs\jergens_facebook\app\plugins\facebook\libs\f_b.php(10): App->import(‘Vendor’, ‘Facebook.facebo…’) #4 C:\xampp\htdocs\jergens_facebook\cake\libs\configure.php(1067): require(‘C:\xampp\htdocs…’) #5 C:\xampp\htdocs\jergens_facebook\cake\libs\configure.php(1043): App->__load(‘C:\xampp\htdocs…’) #6 C:\xampp\htdocs\jergens_facebook\cake\libs\configure.php(962): App->__find(‘f_b.php’, true) #7 C:\xampp\htdocs\jergens_facebook\app\plugins\facebook\controllers\components\connect.php(13): App->import(‘Lib’, ‘Facebook.FB’) #8 C:\ in C:\xampp\htdocs\jergens_facebook\app\plugins\facebook\vendors\facebook\src\facebook.php on line 4”

  7. Nik Chankov Post author

    Hi Ted,

    I am not the original author of the plugin, but from the error which you posted seems like you need to install cURL module to your xampp.

  8. Vikram Acharya

    Actually i want to redirect the disconnect to other function on the controller
    so can u help me a little bit

Leave a Reply

Your email address will not be published. Required fields are marked *