How to adapt PHP library to Codeigniter?

Good guys. My question is a little general: How can I adapt any PHP library (e.g. facebook sdk, for example) for use in CodeIgniter?

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

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

If my question is not yet clear, please let me know.

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

Thanks in advance.

+8
php codeigniter
source share
3 answers
  • create a folder to place facebook-sdk files in it: / application / libraries / facebook /
  • create Facebook_lib.php in the root of the content libraries:

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

  • in the controller:

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

+9
source share

For facebook sdk, you just need to copy the files to the .. / application / libraries / folder, and in the controller you can call it anyway:

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

or

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

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

and in the controller just call your library, for example $this->load->library('facebook');

+1
source share

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

OR

Placement of identically named versions in the folder of your application / libraries.

Classes must have this basic prototype (Note: as an example, we use the name Someclass):

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Someclass { public function some_function() { } } /* End of file Someclass.php */ 

In any of your controller functions, you can initialize your class using the standard:

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

Read more at http://codeigniter.com/user_guide/general/creating_libraries.html

+1
source share

All Articles