How to use Facebook PHP SDK with Zend Framework 2

2019-06-05 22:55发布

问题:

I want to be able to use the Facebook PHP SDK with the new ZEND Framework 2 platform. How to do it?

Zend Framework 2 is quite different as previous version ZF1.

When I try the standard PHP in the main application controller IndexController':

require_once("facebook.php");

class IndexController extends AbstractActionController
{

    public $facebook;

    public function  __construct() {

        $this->facebook = new Facebook(array(
                'appId'  => 'appId',
                'secret' => 'secret_key'
        ));

    }

    /*
    ...
    */

}

I get following error: Fatal error: Cannot redeclare class Facebook in /../module/Application/src/Application/Controller/Facebook.php on line 160

回答1:

I can't help you specifically on the difference between ZF1 & ZF2 but a simple search in google gave me this result, I think because ZF autoloads classes and you tried to require it again they are in conflict.



回答2:

According to that error message the problem is that you now have two classes called Facebook in that namespace. Try ensuring you only have one class called Facebook.



回答3:

I had the same problem when I needed to include the Facebook SDK in a ZF2 application.

You don't need to use any additional modules for FB SDK integration. The trick is to include the facebook library with ClassMapAutoloader, e.g.:

array(
    'Facebook' => 'vendor/FB/facebook.php',
)

Detailed instructions are here