Hassen
Hassen

Reputation: 7644

How to adapt a PHP library to Codeigniter?

Good mor(eve)ning guys. My question is a bit general: How can I adapt any PHP library (like facebook sdk for example) to use in CodeIgniter?

Generally, when you download a PHP library and look to the examples provided, you load the library using include or require_once. What are the adjustments (and ways) to use $this->load->library($name, $params)?

And how can I use the library after that: replacing $var = new Library($data) by ???

If my question is not yet clear, please notify me.

(bonus question: How to apply this to facebook-sdk ?)

Thanks in advance.

Upvotes: 8

Views: 11237

Answers (3)

Mikhail
Mikhail

Reputation: 2562

  1. create a folder to place a facebook-sdk files in it: /application/libraries/facebook/
  2. create a Facebook_lib.php in the libraries root with the content:

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    require_once 'facebook/facebook.php';
    class Facebook_lib extends Facebook{}

  3. in controller:

    $this->load->library('facebook_lib',$config); $this->facebook_lib->clearAllPersistentData();

Upvotes: 9

Duke
Duke

Reputation: 36970

There is nothing stopping you from directly including classes include(APPPATH.'libraries/Facebook/base_facebook.php');

OR

Placing identically named versions in your application/libraries folder.

Classes should have this basic prototype (Note: We are using the name Someclass purely as an example):

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class Someclass {

    public function some_function()
    {
    }
}

/* End of file Someclass.php */

From within any of your Controller functions you can initialize your class using the standard:

$this->load->library('someclass');

More read http://codeigniter.com/user_guide/general/creating_libraries.html

Upvotes: 1

Code Prank
Code Prank

Reputation: 4250

For facebook sdk you just need to copy your files into ../application/libraries/ folder and in a controller you can call it in either ways:

$config = array('appId' => APP_ID, 'secret' => APP_SECRET);
$this->load->library('facebook', $config);

or

create a file named facebook.php in ./application/config folder and create an array in it

$config = array('appId' => APP_ID, 'secret' => APP_SECRET);

and in controller simply call your library like $this->load->library('facebook');

Upvotes: 1

Related Questions