Kartik Social Module

I think I might be having a namespace issue, however I’m stumped because I think I’m calling it correctly. I want to start playing around with FB integration so I can learn how to do user create and login on the advanced template. I started by setting up a simple action on the site controller:




 public function actionFb()

    {

        return $this->render('fb');

    }



then I set up a simple view named fb:




<?php

use yii\helpers\Html;

use kartik\social\Module;


/**

 * @var yii\web\View $this

 */

$this->title = 'FB';

$this->params['breadcrumbs'][] = $this->title;

?>

<div class="site-about">

    <h1><?= Html::encode($this->title) ?></h1>


    <p>This is the FB test page.</p>

    

<?php

        

      

// fetch the facebook sdk api

$facebook = Yii::$app->module->social->getFbApi();


/**

* 

* Get User ID

* @var mixed


$user = $facebook->getUser(); 

var_dump($user);  

 */

    ?>

</div>




You can see I’m using the namespace kartik\social\Module. I’m getting:




PHP Notice – yii\base\ErrorException


Trying to get property of non-object

1. in C:\var\www\yiitry\frontend\views\site\fb.php at line 20


Line 20:

 

// fetch the facebook sdk api

$facebook = Yii::$app->module->social->getFbApi();




For the social plugins, the reason why I’m using this module to call the Facebook object, it has a different namespace:


use kartik\social\FacebookPlugin;

Just to be sure, I tried that here as well and it didn’t work. Any thoughts?

Change this row to:


$facebook = Yii::$app->social->getFbApi();

And you don’t need to declare the use statement here. The module is configured in your configuration file.

Thanks Renka, but it didn’t work. Now I’m getting:




Unknown Property – yii\base\UnknownPropertyException


Getting unknown property: yii\web\Application::social




Have you set up the module in one of your configuration files?

Yes, the module is set up and works for facebook widgets for sharing. I need to declare:




use kartik\social\FacebookPlugin;



Otherwise it doesn’t work, so maybe there is a problem with my config?




<?php

return [

	'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',

	'extensions' => require(__DIR__ . '/../../vendor/yiisoft/extensions.php'),

    'modules' => [

    'social' => [

        // the module class

        'class' => 'kartik\social\Module',

 

        // the global settings for the disqus widget

        'disqus' => [

            'settings' => ['shortname' => 'DISQUS_SHORTNAME'] // default settings

        ],

 

        // the global settings for the facebook plugins widget

        'facebook' => [

            'appId' => 'goes here', 

            'secret' => 'goes here', 

        ],

 

        // the global settings for the google plugins widget

        'google' => [

            'clientId' => 'GOOGLE_API_CLIENT_ID',

            'pageId' => 'GOOGLE_PLUS_PAGE_ID',

            'profileId' => 'GOOGLE_PLUS_PROFILE_ID',

        ],

 

        // the global settings for the google analytic plugin widget

        'googleAnalytics' => [

            'id' => 'TRACKING_ID',

            'domain' => 'TRACKING_DOMAIN',

        ],

        

        // the global settings for the twitter plugin widget

        'twitter' => [

            'screenName' => 'TWITTER_SCREEN_NAME'

        ],

    ],

    // your other modules

],

	'components' => [

		'cache' => [

			'class' => 'yii\caching\FileCache',

		],

        

	],

];



I think there is a slight change in which you need to call this (in case you have not initialized the social module).




$facebook = Yii::$app->getModule('social')->getFbApi();



Also there is a function getFbUser to get FbUser directly. So you can typically do this:




$social = Yii::$app->getModule('social');

$fbUser = $social->getFbUser();

$fbApi = $social->getFbApi();



Note: The other classes like FacebookPlugin are to be namespaced in the ‘use’ statement only if you are using them.

Please post an issue for this on GitHub. Seems like some bug somewhere.

EDIT: The BaseYii class has changed and the module code is now updated to reflect this. You should now be able to work with the change as suggested above (just get the latest code via composer update).

I ran composer update and made the change as suggested. I’m getting the following error:





PHP Fatal Error – yii\base\ErrorException


Class 'kartik\social\Yii' not found


. in C:\var\www\yiitry\vendor\kartik-v\yii2-social\social\Module.php at line 116

111112113114115116117118119120121        }

        if ($secret == null) {

            throw new InvalidConfigException("The Facebook 'secret' has not been set.");

        }

        if (!isset($this->_facebook)) {

            [u]$path = Yii::getPathAlias('@vendor/facebook/php-sdk/src/facebook.php') [/u];

            require_once($path);

            $config = compact('appId', 'secret', 'fileUpload', 'allowSignedRequest');

            $this->_facebook = new Facebook($config);

        }

        return $this->_facebook;

2. yii\base\Application::handleFatalErr



I put [u] tags around the line that was highlighted by the error page. I have at the top of the page:





use kartik\social\Module;



Not sure if that is correct or not.

I am not sure you have the latest version of the code… please compare the \kartik\social\Module on GitHub with the version on your machine. Rerun composer update (or purge the vendor/kartik-v directory and retry the composer update)

Thanks, I deleted the kartik-v directory and reran composer update. No more errors on the page. I’ll start working with the FB example from sdk and see if I can put it all together. Thanks again.