8

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?

  • 2
    Use [Components](http://www.yiiframework.com/doc/guide/1.1/en/basics.component) or take a look at some extensions, like [facebook-opengraph](http://www.yiiframework.com/extension/facebook-opengraph) – bool.dev Mar 07 '13 at 16:30
  • In the past I've installed yii-user extension and then created a new component which extends the yii-user. You can leverage the yii-user to store login tokens. Also I keep my app keys in the config. – aCodeSmith Mar 18 '13 at 22:14

3 Answers3

6

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();

thank you.

rolz
  • 591
  • 2
  • 11
  • 23
0

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 .

bonez
  • 685
  • 1
  • 16
  • 39
0

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

Yii::app()->facebookService->[method_I_defined]
Kirk
  • 1,779
  • 14
  • 20