2

I have a web app using the Facebook api and codeigniter. I currently have about 5 methods in the main controller that use the Facebook library currently all my methods look like

private function getFacebookLogout() { $config = array(); $config['appId'] = '**************'; $config['secret'] = '*****************'; $config['fileUpload'] = false; $facebook = new Facebook($config); $logoutURL = $facebook -> getLogoutUrl(); return $logoutURL; } 

Is there anyway to make this a little cleaner so that I don't have to write those 5 lines of code over and over again

1
  • There are literally about 10 different ways to accomplish this depending on your OOP knowledge. Put it in a private function __construct(){ //$config and stuff } so that it executes as soon as your class is created. Create your own library and invoke it. Create a model to handle the FaceBook stuff so that you can easily call it from other controllers. Commented Mar 14, 2014 at 18:59

1 Answer 1

2

I was in that situation a while back. What I did is create a simple CodeIgniter library to act as a wrapper. Here's the constructor of that library:

class Facebookauth { function __construct() { // Get instance of CodeIgniter object $CI =& get_instance(); // Include the Facebook PHP SDK require_once("facebook/facebook.php"); // Create the Facebook object $config = array(); $config['appId'] = $CI->config->item('facebook_app_id'); $this->appId = $CI->config->item('facebook_app_id'); $config['secret'] = $CI->config->item('facebook_secret'); $this->appSecret = $CI->config->item('facebook_secret'); $config['fileUpload'] = false; $this->facebook = new Facebook($config); } } 

I then just created additional methods to do various tasks, and they could access the Facebook object with $this->facebook. Then, I loaded it as a CodeIgniter library and was able to access it in the usual manner.

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome, thanks, was trying to do this through a config file and that did not work. This is a simple solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.