How to use Facebook PHP SDK with Zend Framework 2

2019-06-05 22:33发布

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

3条回答
ゆ 、 Hurt°
2楼-- · 2019-06-05 23:01

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.

查看更多
ら.Afraid
3楼-- · 2019-06-05 23:02

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.

查看更多
Bombasti
4楼-- · 2019-06-05 23:08

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

查看更多
登录 后发表回答