The Facebook Graph SDK is just a generic PHP SDK. You’ll want to create a service provider in your Laravel application that binds its configuration values.
First, create environment variables named FACEBOOK_CLIENT_ID
and FACEBOOK_CLIENT_SECRET
.
Next, bind those environment variables to configuration values. Open your config/services.php file and add the following:
'facebook' => [
'client_id' => env('FACEBOOK_CLIENT_ID'),
'client_secret' => env('FACEBOOK_CLIENT_SECRET'),
],
Now, use Artisan to create a service provider (php artisan make:provider FacebookServiceProvider
) and add it to the providers
array in config/app.php.
The service provider should look like this:
use Facebook\Facebook;
class FacebookServiceProvider extends ServiceProvider
{
protected $defer = true;
public function register()
{
$this->app->singleton(Facebook::class, function ($app) {
return new Facebook([
'app_id' => $app['config']['services.facebook.client_id'],
'app_secret' => $app['config']['services.facebook.client_secret'],
'default_graph_version' => 'v2.10',
]);
});
}
public function provides()
{
return [
Facebook::class,
];
}
}
This adds a configured instance of the Facebook Graph SDK to your application’s container, meaning you can type-hint it in your classes’ constructors and use it:
class SomeController extends Controller
{
private $facebook;
public function __construct(Facebook $facebook)
{
$this->facebook = $facebook;
}
public function someMethod()
{
// Can use $this->facebook and it’ll already be
// configured with app ID and secret.
}
}
The service provider is also deferred, meaning the SDK is only loaded when you request it.