In this tutorial, we will learn that how we can login with facebook in cakephp . As we see that most of users do not take too much interest in signing up in any website that's why we use social media login by which they can logging or sign up within a single click. For making it work with cakePHP, we first have to->
- Download CakePHP Facebook Plugin from github.com/webtechnick/CakePHP-Facebook-Plugin
- After downloading , just copy the foler in cakePHP plugin folder "app/plugins/facebook"
- Make sure that you are already using the Authcomponent CakePHP Facebook Plugin.
2.Setup Facebook Plugin
when we copy plugin in cakePHP plugin folder,Copy and rename file "app/plugins/facebook/config/facebook.php.example" to "app/config/facebook.php". After creating this, we need to create a fecbook app in facebook website for our website.The app key should be must to run the facebook login in cakephp.
3.Setup Database
If you are already having a database then you need to add one more field "facebook_id" to the users table or any table you are using.
4.Setup Controller
Under userscontroller.php . you can copy the following code. Please check the code if you have already made some of the functions.
<?php
class UsersController extends AppController {
var $name = 'Users';
var $components = array('Facebook.Connect','Auth'); //1
var $helpers = array('Facebook.Facebook'); //2
function beforeFilter() {
$this->Auth->loginRedirect = array('action' => 'index'); //3
$this->layout='facebook'; //4
}
function index() { //5
}
function login() { //6
}
function logout() { //7
$this->Session->destroy();
$this->redirect($this->Auth->logout());
}
}
?>
In the above code we have used
- "Connect" component from Facebook Plugin and "Auth" from core files.
- "Facebook" helper from the Plugin.
5.Setup Layout
We have called facebook layout in the controller. If you want you can call and create the layout for facebook and can write the layout code there.
6.Setup View
The most important part is now in the login display file.Create two view files "app/views/users/login.ctp" and "app/views/users/index.ctp"
<?php
echo $this->Facebook->logout(array('redirect' => array('controller' => 'users', 'action' => 'logout')));
?>
login.ctp
<?php
echo $this->Facebook->login(array('width' => '174','height'=>'25','scope' => 'email'),
__('Login with Facebook',true));
?>
0 Comment(s)