Skip to content
Advertisement

Adding Facebook Library in Codeigniter 4

I am Using CI 4 for my project and I need Facebook Login to get user data which I have done in fb-JS-sdk and after that I save data to DB and start session for that user so It looks like it’s directly loged in by facebook login, but that’s not point here,

Now After Login with Facebook user will donate from payment gatway and it will be redirected to success page which will show thank you for payment message, now I want to add more on success page = I want to post on user’s FB wall with message that “I have just donated on site for foo reason…blah…”.

I know I need Fb user token for that which is I have already during fb login from js-sdk saved in DB.

I am very new to codeigniter and don’t know How to add facebook library and use/initialize it in my controller. I am not using composer.

So I added fb-php-sdk into my app/Libraries (downloaded from github)

So I tried:

<?php 
namespace AppControllers;
use CodeIgniterController;
use AppLibrariesFacebook;

class User extends BaseController {

    public function __construct(){

        helper('url','form');
        
    }

    public function success() {
        require_once ('Libraries/Facebook/autoload.php');
        $fb = new Facebook(); // loads and creates instance
        
        $a = $fb->getDefaultGraphVersion();
        print_r($a);

        $session = session();

        $page = [
            'name' => 'success_payment',
            'title' => $_SESSION['fname']." ".$_SESSION['lname']." Thank You - ".SITENAME,
            'fb_token' => $_SESSION['fb_token'],
        ];

        echo view('templates/header', $page);
        echo view('success_payment'); //this template have success msg for successful payment..thank u and all
        echo view('templates/footer');

    }

}

I am Getting Errors.

Warning: Uncaught ErrorException: require_once(Libraries/Facebook/autoload.php): failed to open stream: No such file or directory in C:xampphtdocsbbappControllersUser.php:55 Stack trace: #0 C:xampphtdocsbbappControllersUser.php(55): CodeIgniterDebugExceptions->errorHandler(2, 'require_once(Li...', 'C:\xampp\htdocs...', 55, Array) #1 C:xampphtdocsbbappControllersUser.php(55): require_once() #2 C:xampphtdocsbbsystemCodeIgniter.php(918): AppControllersUser->success() #3 C:xampphtdocsbbsystemCodeIgniter.php(404): CodeIgniterCodeIgniter->runController(Object(AppControllersUser)) #4 C:xampphtdocsbbsystemCodeIgniter.php(312): CodeIgniterCodeIgniter->handleRequest(NULL, Object(ConfigCache), false) #5 C:xampphtdocsbbpublicindex.php(45): CodeIgniterCodeIgniter->run() #6 {main} thrown in C:xampphtdocsbbappControllersUser.php on line 55

Fatal error: AppControllersUser::success(): Failed opening required 'Libraries/Facebook/autoload.php' (include_path='C:xamppphpPEAR') in C:xampphtdocsbbappControllersUser.php on line 55

can Anybody Guide me how to intgrate custom library in CI4.?? Sorry for not using composer as well as poor english.

Advertisement

Answer

Easiest way to load the library, specially in Codeigniter 4 that fully supports composer is using composer.

In your project just do a simple:

$ composer require facebook/graph-sdk

After that your library will be auto-loaded so you should just declare the proper namespace to use it on your controllers.

In the case you can’t really use composer you actually did the first bit right and added the facebook library to you App/Libraries folder or your app/third_party foder. However in this case you need to either register said library on your autoload or just load it manually on your controller.

In case you have it in your third_party folder you can autoload it like so:

$classmap = [
    'Facebook' => APPPATH .'third_party/_insertFilenameHere_.php'
];

If neither of the above methods finds the class, and the class is not namespaced, the autoloader will look in the /app/Libraries and /app/Models directories to attempt to locate the files.

If you opt by adding the library to your Libraries folder then you need to add that to your autoload too.

$psr4 = [
    'App'         => APPPATH,                // To ensure filters, etc still found,
    APP_NAMESPACE => APPPATH,                // For custom namespace
    'Config'      => APPPATH . 'Config',
    'Libraries'   => APPPATH . 'Libraries'   // Your custom Libraries
];

This way all libraries in your App/Libraries folder will be ready to use.

This is why I would go to the third_party code and not the libraries. The libraries folder is normally used for classes that you built for your application and not full blown third party code. That should be in a composer package or the third party folder.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement