I have put the Facebook SDK files into the
protected/helpers directory, from which I autoload classes and 3rd Party helpers for my Yii Application.
When one of my projects involved Facebook Login, I’ve came up with this simple and quick solution, using the
Yii::app()->params to achive the inheritance of an object property throughout the application.
// don't mind this $facebook variable, it's just a shortcut in this method Yii::app()->params['facebook'] = $facebook = new Facebook(array( 'appId' => '148966221932337', 'secret' => 'a52ce7b4a0cd5d517c6ada53fc77cde7', 'cookie' => true, )); $user = $facebook->getUser();
Thereby, I have the
Yii::app()->params['facebook'], set or unset, available throughout my application and in that way I can access the Facebook API SDK wherever in my application, however I do belive that this is not a good solution.
Are there any, better, solutions for implementing Facebook API SDK into the Yii Framework Application?
Ya. download Facebook SDK files and Keep it in protected/components/Facebook then edit your config/main.php with
'import'=>array( //autoloading model and component classes 'application.models.*', 'application.components.*', 'application.components.Facebook.*', ),
and add appid andsecret key in your param
'params'=>array( 'Facebook'=>array( 'appId' => '148966221932337', 'secret' => 'a52ce7b4a0cd5d517c6ada53fc77cde7', 'cookie' => true, ), );
From you page you can call your Facebook SDK like this
$facebook = new Facebook(Yii::app()->params['Facebook']); $user_id = $facebook->getUser();
I always try to store config variables in the DB. Especially if they involve a secret key. I would use Yii’s crypt functionality and store it in the db. Then I would create model around retrieving config variables like that.
Secondly as far as the actual connection work and what have you, I would put that as a Component which extends CApplicationComponent, and either place the file in the extensions directory or the components directory. If you want to make it a true object. I think the helpers idea is a just a set a loose functions, mostly I use helpers for array and string work .
Kind of late, but I ended up downloading the Facebook SDK files with composer, autoloading those, and created a FacebookService extending CApplicationComponent that wraps the SDK.
So the main config is like
'facebookService'=>array( 'class' => 'FacebookService', 'appId' =>'xxxxxxxxxxx', 'appSecret' => 'xxxxxxxxxx', 'extendedAccessToken' => "xxxxxxxxxx", ),
and you can use it like