Adding Facebook Library in Codeigniter 4 Adding Facebook Library in Codeigniter 4 codeigniter codeigniter

Adding Facebook Library in Codeigniter 4


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.