how to login using facebook in zend framework 2

Oct 29, 2012   //   by kdecom   //   facebook, facebook connect, facebook login, zend framewok 2, zend framework tutorial  //  8 Comments
Zend Framework 2 and Facebook Connect Example
Zend Framework 2 and Facebook Connect Example

To do that we need to first create an app in Facebook:-

Follow this steps to create an app in facebook

  1. Open this page http://developers.facebook.com/
  2. Click on “Apps” You can find that above right corner.
  3. Click on ” CREATE NEW APP” button.
  4. Write down your app name and app namespace.
  5. Click Countinue.
  6. Enter Captcha.

Once you follow this steps then if will take you to the app edit page.

Then you need to write down your site Url something like www.example.com or you can use www.localhost.com as well if you just developing on your local machine. On that page you will get two things.

App ID
App Secret 

Once you got APP ID and APP Secret its good news now we ready to start coding.

First thing we gona do is create a Facebook login button.
We have to add new action inside a IndexController like this

ZF2_APPLICATION/module/Application/src/Controller/IndexController.php

[php]public function fbLoginAction() {

$facebook = new Facebook(array(
‘appId’ => ‘229416993799411’,
‘secret’ => ‘f9b868e7a9c955f15bff59561f30a192’,
));
$url = APPLICATION_URL . ‘/application/index/fb-return’;
return new ViewModel(array(
‘login_url’ => $facebook->getLoginUrl(array(‘redirect_uri’ => $url, ‘scope’ => ’email,publish_stream, offline_access’)),
‘title’ => ‘Facebook Login’
));
}

[/php]

And Create a view file for this action

ZF2_APPLICATION/module/Application/view/application/index/fb-login.phtml

Now to let the Facebook SDK works we need to add them into our library code so how to do it. If you not sure how to add your library into a Zend Framework 2 then follow this post.

Now when someone click on the Facebook login button it will open the facebook popup and redirect back to our site.

So in that url we will just post on user wall.

So we need to add a new action here.

[php]
$facebook = new Facebook(array(
‘appId’ => ‘229416993799411’,
‘secret’ => ‘f9b868e7a9c955f15bff59561f30a192’,
));

$user = $facebook->getUser();
if($user == 0) {
$url = APPLICATION_URL . ‘/application/index/fb-login’;
return $this->redirect()->toUrl($url);
}
$token = $facebook->getAccessToken();
$me = $facebook->api(‘/me’);
$facebook->api(‘/me/feed’, ‘post’, array(
‘message’ => ‘Zend Framework 2 Facebook Wall post Example’,
‘name’ => ‘Kdecom’,
‘description’ => ‘Zend Framework 2 Facebook login and wall post example…’,
‘link’ => ‘http://kdecom.blogspot.com’,
‘access_token’ => $token));
return new ViewModel(array(
‘logout_url’ => $facebook->getLogoutUrl(),
‘title’ => ‘Welcome to’ . $me[‘name’]
));
[/php]

In the view file what we will do is:-

I hope it make sense.
Well if you want you can add the user into a database as well in this action and email to the user.

To add the user into Database you have to checkout my zend framework 2 database interaction post.

If anyone has any problem or any comment please post it on comment so i will help me to improve post.

thanks in advanced.

[freebiesub download=”http://www.kdecom.com/wp-content/uploads/2012/10/zf2-facebook-login-wall-post.zip”]

8 Comments

  • Download link for the source does not work 🙁 !

    • sorry to all who got the trouble downloading link.. But it’s been fixed now.

      Thanks

  • What is “APPLICATION_URL”? My app is e.g. http://www.myapplication.loc/application/index/fb-login and my Facebook app is http://apps.facebook.com/appname and i dont know if i should use the http://www.myapplication.loc/ for Canvas URL on Facebook settings? Becouse it’s not working, on apps.facebook.com/appname i have error 404.

    • Hi Sorry but application_url is your root domain name or root URl so in your case
      Just add a line in your index.php
      define(‘APPLICATION_URL’, ‘ http://www.myapplication.loc/‘);

      If it still dont work let me know we try to help you.

      Thanks

  • That’s rather a way to register a user with his facebook account.
    For login procedures one would also need to code an AuthAdapter and build the session out of it.

    • Yes that was my next step. But havent got a time to update the code.

      for authentication if you want you can use this module which is for zf2.

      https://github.com/EvanDotPro/EdpUser

  • Hello,

    I have problem with fb login. I got error like Fatal error: Class ‘Kdecom\Facebook’ not found.
    can you give me the how to put Facebook,BaseFacebook in library.

    Thank You

Leave a comment

css.php