Sharing how I implemented the Facebook SDK V4 on Laravel 4.
Here is what I added by default composer.json
"autoload": { "classmap": [ "app/commands", "app/controllers", "app/models", "app/database/migrations", "app/database/seeds", "app/tests/TestCase.php" ], "psr-4" : { "Facebook\\":"vendor/facebook/php-sdk-v4/src/Facebook/" } },
Added Facebook initialization function on my index.php, for example:
/* |-------------------------------------------------------------------------- | Initialized Facebook PHP SDK V4 |-------------------------------------------------------------------------- | */ //Initialize use Facebook\FacebookSession; FacebookSession::setDefaultApplication(Config::get('facebook.AppId'),Config::get('facebook.AppSecret'));
And for a session, Laravel does not use $ _SESSION, so you don’t have to do session_start at all. In order for you to use the Laravel session on the Facebook PHP SDK V4, you need to extend the Facebook class FacebookRedirectLoginHelper. Here's how to subclass FacebookRedirectLoginHelper and overwrite session processing.
class LaravelFacebookRedirectLoginHelper extends \Facebook\FacebookRedirectLoginHelper { protected function storeState($state) { Session::put('state', $state); } protected function loadState() { $this->state = Session::get('state'); return $this->state; } protected function isValidRedirect() { return $this->getCode() && Input::has('state') && Input::get('state') == $this->state; } protected function getCode() { return Input::has('code') ? Input::get('code') : null; }
And one more step, you need to execute the composer command to regenerate startup files:
composer dump-autoload -o
Well, if everything goes right, now you are ready to start using the SDK, here is a sample
Here is an excerpt from one of my project classes:
namespace Fb\Insights; //Facebook Classes use Facebook\FacebookSession; use Facebook\FacebookRequest; use Facebook\FacebookSDKException; //Our Facebook Controller use FbController; class PagePosts extends \Facebook\GraphObject { /* Get Page Posts Impression https://developers.facebook.com/docs/graph-api/reference/v2.0/insights
Feel free to comment or suggest so that I can improve my code. Happy coding.