The Controller and View

Let's create a sample controller to test out our model. Copy and paste the code below, and save it as facebooktest.php in your controllers folder.


<?

class Facebooktest extends Controller{
	function Facebooktest(){
		parent::Controller();
		$this->load->model('facebook_model');
	}
	
	function index(){
		$this->load->view('facebooktest/index');
	}

	function test1(){
		$data = array();
		$data['user'] = $this->facebook_model->getUser();
		$this->load->view('facebooktest/test1',$data);
	}
}

?>

And of course we will need to create a view. Create a new folder in your views folder and name it facebooktest. Then save the code below as test1.php in your views/facebooktest folder.


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<title>Kent is Learning CodeIgniter - Test 1</title>
</head>
<body>
	
<fb:login-button autologoutlink="true" 
			onlogin="window.location.reload(true);"></fb:login-button>

<?
	if(isset($user)){
?>
		<p style='background-color:yellow;'>Right click and view page
		source to see a more readable screen</p>
<?
		print_r($user);		
	}
	else{
?>
		<p>If you don't log in, you can't see the magic</p>
		
<?
	}
?>



<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
  FB.init({appId: '131334533555448', status: true, cookie: true, xfbml: true});
  FB.Event.subscribe('auth.sessionChange', function(response) {
    if (response.session) {
      // A user has logged in, and a new cookie has been saved
	window.location.reload(true);
    } else {
      // The user has logged out, and the cookie has been cleared
    }
  });
</script>
</body>
</html>

Okay, there are a couple of things that are important in this view. One is this bit: xmlns:fb="http://www.facebook.com/2008/fbml. xmlns stands for XML Name Space. You can read more about it from the really smart guys at W3. Before trying to connect Facebook with Codeigniter, I never really paid attention to xmlns. But you need it for your Facebook login button to work.

The 13 lines right above the closing body tag are important too. Without it, your Facebook login button won't work either. I won't pretend I understand what that little bit of JavaScript does, but it is necessary. Facebook says to put it at the bottom of your page. When I tried putting it in the <head> portion of the document, it didn't work. I guess the developers at Facebook know are smarter than me.

Speaking of the Facebook login button, if you're like me, you've never seen a tag that looks like <fb:login-button> before. That's part of the xmlns magic. The autologout attribute keeps the button from saying "log in" when the user is already logged in. You can remove it if you want, but I don't know why you'd do it except to see if I really know what I'm talking about. And the other attribute reloads the page when you log in. This is also optional; you can leave it out if you want to. I have it in because the page displays different information depending on whether or not the user is logged in. Without it, the user can log in, but the page won't refresh, and the information that shows for a logged-in user won't show unless they manually reload the page.

If you want to see this code in action, you can see it here. You'll have to log in to see it. The page displays the user array, so you can see what information is available about the user.

Of course, any seasoned veteran of CodeIgniter knows if I just wanted to display the user's name instead of the whole user variable, I could have written <?=$user['name'];?> in the view. <?=$user['hometown']['name'];?> would display the user's home town. You're probably smart enough to figure out the rest.

So getting some useful information from a user isn't too tricky, is it. (Well, it was for me, the first time around, but hopefully I explained it well enough so it's easy for anyone reading this.

Let's look at getting information about a user's Facebook friends.

Go to page 4

Comments

Eric

15 July 2010 2:24 am

Hi Kent, I'm following and going through your tutorial. Everything is good, but the $user object is not populated with data for some reason. When I login via Facebook, it reloads the page and then the facebook button changes from "login" to "logout", but I checked and the $user object is not there. Any idea why? Do I have to use API Key at all?

Kent

16 July 2010 12:23 pm

Note on Eric's comment: Eric and I emailed each other on this issue. He found a solution to his problem that I placed on page 5 of this tutorial.

v1r

25 August 2010 12:34 pm

Hi Kent, nice tutorial.. but i have the same problem as eric... im trying to integrate your tutorial with pyrocms.. Thanks in advance!

Your comment will appear once it is approved.

  • (required)
  • (required - will not be published)
  • (optional)