Reputation: 9648
I'm new to PhalconPHP and I'm trying to integrate the Facebook php-sdk into and I'm not sure how to make it so that I can access the $facebook and $user variables on every page of my app (or redirect if need be) so far in my bootstrap file I have the below:
try {
//Register an autoloader
$loader = new \Phalcon\Loader();
$loader->registerDirs(array(
'../app/controllers/',
'../app/models/',
'../app/facebook/'
))->register();
//Create a DI
$di = new Phalcon\DI\FactoryDefault();
//Setting up the view component
$di->set('view', function(){
$view = new \Phalcon\Mvc\View();
$view->setViewsDir('../app/views/');
return $view;
});
//setup facebook
$config = array();
$config['appId'] = 'xxxxxxxxxxxxxxxxxxxxxxx';
$config['secret'] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$facebook = new Facebook($config);
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 2;
$user = null;
try{
$user = $facebook->getUser();
}catch(FacebookApiException $e){
$user =null;
//echo "FacebookApiException :". $e;
}
$di->set('facebook', function() {
global $facebook; // obviously don't want this here
return $facebook;
}, true);
$di['user'] = $user';
// $di->set('user', function(){
// return $['facebook']->getUser();
// });
// $di['facebook']=$facebook;
// $di['user']=$user;
//Handle the request
$application = new \Phalcon\Mvc\Application($di);
echo $application->handle()->getContent();
} catch(\Phalcon\Exception $e) {
echo "PhalconException: ", $e->getMessage();
}
Now the $facebook variable is now accessible by using
$this->facebook
on a controller and as such I can pass it to a view. But what's the best way of being able to access $user from a controller as using set('name',$var); doesn't seem to be valid. Am I best just doing something such as
$di->set('facebook', function(){
//setup facebook config here
return new Facebook($config)l
});
or should I be going about this another way i.e should I create a User component that returns the facebook user?
Should I follow the approach here Global acssess to some component in Phalcon which seems to suggest the following is valid
// Store it in the Di container
$this->di['config'] = $config;
** edit
using
$di->set('user',function() use($user){
return $user;
});
achieves what I want, but if I do
$di->set('user', $user);
I can't access it, Can anyone explain what's going on really?
thanks
Upvotes: 0
Views: 6045
Reputation: 1531
Subclass Controller like so:
class ControllerBase extends Controller
{
protected $user;
protected function beforeExecuteRoute($dispatcher)
{
...
$user = $facebook->getUser();
...
}
}
You controller can inherit from ControllerBase instead of Controller and use $user in any of the action functions.
Upvotes: 1
Reputation: 662
I would not use session, but rather a session bag to store user, as it's a bit more flexible.
And to answer your question of 'what's going on'. I'm no expert, but from experience it seems to me that Phalcon's DI returns objects and not primitive types. You are perfectly fine doing :
$cl = new stdClass();
$cl->hello = 'hi';
$di['hello'] = $cl;
$di->get('hello')->hello; // 'hi'
but trying to do as you are:
$di['bye'] = 'bye';
$di->get('bye');
will give you an exception 'Service 'bye' cannot be resolved'. ('Missing 'className' parameter' if you pass an array)
Conclusion:
$user
to a stdClass, orUpvotes: 0
Reputation: 173
Why don`t you use sessions?
For example:
$di->set('session', function() {
$session = new SessionAdapter();
$session->start();
$session->set('user', array(
'fb'=> new Facebook()
));
return $session;
});
And then extend your view and base controller which retrieves fb session and stores it as static variable.
Upvotes: 1