Yii Framework Forum: Kartik Social Module - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Kartik Social Module problem calling getFbApi method

#1 User is offline   evercode 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 151
  • Joined: 23-December 12

Posted 22 March 2014 - 04:10 PM

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?
0

#2 User is offline   Renka 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 06-April 12

Posted 23 March 2014 - 05:43 AM

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.
0

#3 User is offline   evercode 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 151
  • Joined: 23-December 12

Posted 23 March 2014 - 12:23 PM

View PostRenka, on 23 March 2014 - 05:43 AM, said:

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


0

#4 User is offline   Renka 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 06-April 12

Posted 23 March 2014 - 03:31 PM

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

#5 User is offline   evercode 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 151
  • Joined: 23-December 12

Posted 23 March 2014 - 06:49 PM

View PostRenka, on 23 March 2014 - 03:31 PM, said:

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',
		],
        
	],
];

0

#6 User is offline   Kartik V 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 650
  • Joined: 29-August 12

Posted 24 March 2014 - 05:06 AM

View Postevercode, on 22 March 2014 - 04:10 PM, said:


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

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).

This post has been edited by Kartik V: 24 March 2014 - 05:36 AM

0

#7 User is offline   evercode 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 151
  • Joined: 23-December 12

Posted 24 March 2014 - 01:16 PM

View PostKartik V, on 24 March 2014 - 05:06 AM, said:

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.
0

#8 User is offline   Kartik V 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 650
  • Joined: 29-August 12

Posted 25 March 2014 - 01:37 AM

View Postevercode, on 24 March 2014 - 01:16 PM, said:

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

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)
0

#9 User is offline   evercode 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 151
  • Joined: 23-December 12

Posted 25 March 2014 - 12:07 PM

View PostKartik V, on 25 March 2014 - 01:37 AM, said:

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users