How to build Facebook login using CakePHP Facebook Login Plugin
15 Feb 2012 Add Comments Intermediate LevelFacebook connect enables users to login to your application instantly with a Facebook Account. It saves user from checking email to verify his account, which will greatly enhance your application's user experience.
In this tutorial, we will show you how to implement Facebook login for you CakePHP application using a great plugin "CakePHP Facebook".
Table Of Content
Content
1.Prepration
- Download CakePHP Facebook Plugin from Github.
- Unpack downloaded folder, and copy it to directory "app/plugins/facebook"
- We are using CakePHP 1.3.2 in this tutorial.
- CakePHP Facebook Plugin is integrated with CakePHP Auth component, so we will assume you are using Auth component for your application.
- For simplicity, in this tutorial, we will only have one user controller, and after user logins, it will show a demo home page, and inside the home page, there is a logout button.
2.Setup Facebook Plugin
Copy and rename file "app/plugins/facebook/config/facebook.php.example" to "app/config/facebook.php". And then you will need to create a Facebook App, and supply its appId,apiKey, secret here. Please note this is required for Facebook login to work.
3.Setup Database
You may already have a database set up if you are trying to implement Facebook login in your existing project.
In this demo, we will have a simple users table as below. Take note if you are doing this in a existing database, you will need to add one more field "facebook_id" to the users table.
If you are strictly following this tutorial, please import sql below to your database to create a users table
4.Setup Controller
Create users_controller.php under /app/controllers folder.
- Include "Connect" component from Facebook Plugin and "Auth" from core files.
- Include "Facebook" helper from Facebook Plugin.
- We want user to be redirected to index page after successfully login.
- We will have to use a special layout file for our views, which will be created next step.
- This will be the home page after users has login.
- Dummy login function for Auth component.
- Inside logout function, we explicitly call Session->destroy() to delete the whole session variable, this is due to a bug, which Facebook will always try to fetch its content via a session variable, so we will have to explicitly delete the whole session.
5.Setup Layout
Create a layout file "app/views/layouts/facebook.ctp" and copy the content below to the file.
Nothing special about this file, except we are calling Facebook helper at the beginning and end of it. This basically will include needed Javascript from Facebook and invoke some specific Javacript calls to make the Facebook login and logout button work.
6.Setup View
View pages are the most important part of setting up Facebook login, please pay attention in this section.
Create two view files "app/views/users/login.ctp" and "app/views/users/index.ctp"
- login.ctp
Here is the magic happens, Facebook->login() will create a Facebook login button for us, and when user clicks on it, it will prompt user to login using Facebook account, and when that is done. Facebook Connect component will automatically create a user account for this user and let him login using Auth component.
- index.ctp
This page is simple, but it is also the most important. Here we use Facebook helper to render a logout button.
Pay attention to the "redirect" parameter of the method.
Keep in mind, Facebook logout button will only log user out of Facebook, it is your responsibility to
log them out of your application, thus you have to redirect user to logout method.
7.The End
Now you should have a simple authentication application powered by Facebook. You should be able to login and logout of the application using you Facebook account. It does not do much, but it should have shown you how to use Facebook Connect for your application. Below are some screenshots of the tutorial:
Login page
Index page
Hopefully this simple tutorial helped you with your development.
If you like our post, please follow us on Twitter and help spread the word. We need your support to continue.
If you have questions or find our mistakes in above tutorial, do leave a comment below to let us know.



77 Comments
a nice tutorial, thanks for share this, now i dev a project ues CakePHP Facebook. but not cakephp 1.3, i use cakephp 2.x
you can share CakePHP Facebook for cakephp 2.0? thanks for that
Please send us an email, we can send you a CakePHP 2.1 version of the script.
Regards
XD
Please send me CakePHP 2.1 version of this script, i will be really thankful.
hi,
you can please send me the script for the CakePHP 2.x version
thanks
I also need a facebook connect code for the cakephp 2.1. If you have some code please send me. my email is dilipstudy@gmail.com
Hi
We have sent you the copy. :)
Regards
XD
Hi,
Thank You very much for your help. It works fine. If you need any types of help in future please contact us.
Thanks & Regards,
Dilip
I also need the facebook connect code for cakephp 2.3
Thanks :)
Hi,
can you please send me the script for CakePHP 2.3.1. Thank you in advance :)
regards
Please send me the CakePHP 2.1.3 version of Facebook Connect plugin..
Thanx!!!
Please send me the CakePHP version 2.1.3 of this plugin. And a step by step tutorial if possible. I am new to cakephp.
Thanks!!!
I tried for cake 2.0 , its giving a facebook icon and when clicked on it
"Given URL is not allowed by the Application configuration."
thank u for the plugin but please can yo send to me the one for version 2.0...
Hi webtechnick
Hope you are doing well
I have problem using your plugin
i have place all files in this directory
C:\wamp\www\cak2\app\plugins\facebook
and one file is copy and place config and set api key and other information
C:\wamp\www\cak2\app\config\facebook.php
Now i have create a controller
class UsersController extends AppController {
var $name ='Users';
var $helpers = array ('Session','Html','Form','Facebook.Facebook');
var $components = array('Auth');
function login() {
}
}
?>
But Missing Helper File
Error: The helper file app\views\helpers\facebook.php can not be found or does not exist.
Error: Create the class below in file: app\views\helpers\facebook.php
class FacebookHelper extends AppHelper {
}
?>
This error is there
if i right this line in bootstrap.php
CakePlugin::load('Facebook');
then this error is come Fatal error: Class 'CakePlugin' not found in C:\wamp\www\cak2\app\config\bootstrap.php on line 51
I am using 1.3 cakephp please suggest me where i am worg
Hi, this website is actually not owned by webtechnick.
You do not need to do "CakePlugin::load('Facebook');" this is for Cakephp 2.x only.
Regards
XD
Please send me the CakePHP 2.0 version of Facebook Connect plugin..
Hi
We have emailed it to you.
Cheers.
Regards
XD
Hi XD,
can you please send me the CakPHP 2.0 version, too?
Thx in advance!
Hi
We have sent you the copy.
Cheers
XD
Please send me the facebook connect plugin for cakephp2.x
Hi,
We have sent you the copy.
Cheers
XD
Please can i gen plugin for new cakephp 2.x?
THX
Hi
We have sent you the plugin.
Regards
XD
I need a facebook connect code for the cakephp 2.2 If you have please send me and steps if possible. my email is moh.saied.fci@gmail.com
Hi
I have sent you the copy.
Regards
XD
Can anyone send me the facebook connect mode for CakePHP 2.2 please!
Thanks a lot in advance!
Hi
We have sent you the plugin.
Cheers.
XD
Please send me a copy for cake 2.x.... Thnks in adv
Hi
We have sent it to you.
Cheers
please send me the working 2.2.4 version of the plugin
Hi
We have sent it to you
Cheers
XD
Hi,
Xu Ding
Please send me 2.x version.
Thank You in advance.
Hi
We have sent to you
Cheers.
XD
can you send me a copy for 2.1 too?thanks in advance
Hi,
WE have sent you .
Cheers
XD
Hi, can you send me a copy for 2.1 too?
thanks a lot
Hi
We have sent you a copy.
Regards
XD
Hello Xu Ding,
The plugin works really cool.
I am having one issue with this plugin.
When I setup controller as in step 4.
point no 3: We want user to be redirected to index page after successfully login.
it is not working for me. After successful login my cake application stucks on login page.
Though the page shows me that i have been logged in successfully.
but i can not access logout link.
My canvas url : http://localhost/mylocalfacebook/users/index/
Please help me on this.
Regards,
Mrudul Shah
I believe there is a callback for doing this.
Check it out in the code.
Regards
XD
Hi and thank you for your wonderful tutorial..
can you send also the howto for version 2.1 to my email
Many thanks again
Hi
We have sent to you
Regards
XD
Hi ! I followed the guide but I've this error :
Fatal error: Cannot use string offset as an array in /home1/mysite/public_html/cake/libs/configure.php on line 992
Have any suggestion for me ?!
Thx in advance !
hi friends. maybe I understand why it not works for me ... I'm using cakephp 1.2 . Do you have a version of the plugin for cakephp 1.2.6 ? if yes, could send it to my email ? Thx in advance !
No, we do not have version for 1.2.6. :(
Hopefully anyone in the comments have one.
plz send me cakephp 2.2.2 facebook login plugin..
Hi,
We have sent it to you.
Please check.
Cheers.
XD
Hello,
Please send me the Plugin for V. 2.3
Thx.
Joh
Hi
Plugin sent.
Cheers
XD
Hi dude.
Hey please, can you send me the plugin for V.2.3.0?
I really need it.
Big Thanks.
Hi
Email is sent
Cheers
XD
Would you please send me the plugin for v.2.3?
Thanks a lot!
Hi
Email sent
Cheers
XD
How to send content to my wall or my organization's page with this plugin? Is it even possible with the plugin?
HI
That is not in the scope of this plugin I am afraid.
Regards
XD
Can I have a copy for cakephp 2.3.1?
Thank you in advance!
Hi
Plugin sent.
:)
Cheers
XD
Could you send me a link for current version of Cake?
Thanks in advance.
Pablo.-
Hi
We have sent you the plugin
Cheers
XD
Hii,
Can you please sent me plugin for cake 2.0
Thanks a lot.
Hi
We have sent you the plugin
Cheers
XD
Hello, could you please send me the plugin? I'm using cakephp 2.3.1
Thank you!!
Hi
We have sent you the plugin
Cheers
XD
Thank you very much
Hello, can you send me the plugin for Cakephp 2.X please ?
Thanks you!
Hi
Plugin sent.
Cheers
XD
Hi,
Can you please sent me plugin for cake 2.0
Thanks a lot.
Hi
Plugin sent.
Cheers
XD
Hi,
Can you please send me the script for cake 2.3.1?
Thanks!
Hi,
can you send me a version for Cake 2.2.4 please?
Thanks a lot!
Does it work on CakePHP 2.3.1?
Could you please send me the script for it. Thank you
Hi
Can you send me a copy for Cake 2 please, thanks.
Could you please send me the script for cakePHP 2.3.1 version..It will help me very much.
Could you please send me the script for Cake 2.x?
Thanks for this tutorial. Please send me the facebook connect plugin for cakephp2.x. my email: dinesh.amstech@gmail.com
Thanks for this tutorial. I am working on cake 2.2.3. Could you please send me the plugin that works for 2.2.3
Hi,
Can you send me the script for cake 2.3
Thanks
please send me the cake 2.x version