我在网上发现的一些评论建议应该包含带require_once()的库:
http://forge.typo3.org/issues/33142
- if it’s just a tiny helper library, it’s intended to be stored in {PackageRoot}/Resources/PHP/{libraryName} and just included via require. is this suspected by the problem however?
- if the FLOW3 package mainly represents the foreing library at all, like it’s the case in Imagine or Swift package, the library code is put below {PackageRoot}/Classes directly.”
http://lists.typo3.org/pipermail/typo3-project-typo3v4mvc/2011-July/009946.html
“I would include the class (using require_once) from within a specific action to handle this. That way you have access over those functions and the class becomes your library.”
我试过这个,它的工作原理如下:
<?php
require_once( t3lib_extMgm::extPath('extkey') . 'Resources/PHP/facebook-php-sdk/facebook.php');
class Tx_WsLogin_Domain_Repository_FacebookUserRepository extends Tx_WsLogin_Domain_Repository_UserRepository {
protected $facebook;
public function __construct() {
$this->setFacebook(new Facebook(array(
'appId' =>'',
'secret' => '')
));
parent::__construct();
}
public function setFacebook(Facebook $facebook) {
$this->facebook = $facebook;
}
public function sampleFunction() {
$userId = $this->facebook->getUser();
}
}
?>
但是如何让它自动加载并使用injectFacebook函数自动注入库?
编辑:
像@alex_schnitzler和@sorenmalling提到的关于自动加载:
@PeterTheOne Put all the files inside ext_autoload.php and then use DI or the object manager.
@PeterTheOne put the class definition into ext_autoload.php in your extension?
我试过这样的(文件:ext_autoload.php):
<?php
$extPath = t3lib_extMgm::extPath('extKey');
return array(
'facebook' => $extPath . 'Resources/PHP/facebook-php-sdk/facebook.php',
);
?>
它似乎找到并包含正确的文件.但是当我尝试使用依赖注入(如peter answered)时,我收到一个错误:
not a correct info array of constructor dependencies was passed!
InvalidArgumentException thrown in file /var/syscp/webs/web1/dev/typo3_src-4.5.15/typo3/sysext/extbase/Classes/Object/Container/Container.php in line 247.
我想这是因为Facebook类的构造函数有一个必需的$config参数.
EDIT2:
我做了peter在他的回答中所说的,并且在@alex_schnitzler和@sorenmalling的帮助下,我指向了ObjectManager,我的FacebookService现在看起来像这样:
class Tx_Extkey_Service_FacebookService implements t3lib_Singleton {
/**
* @var Tx_Extbase_Object_ObjectManagerInterface
*/
protected $objectManager;
/**
* Facebook from @link https://github.com/facebook/facebook-php-sdk facebook-php-sdk
*
* @var Facebook
*/
protected $facebook;
/**
* @param Tx_Extbase_Object_ObjectManagerInterface $objectManager
*/
public function injectObjectManager(Tx_Extbase_Object_ObjectManagerInterface $objectManager) {
$this->objectManager = $objectManager;
}
/**
*
*/
public function initializeObject() {
$this->facebook = $this->objectManager->create(
'Facebook',
array(
'appId' =>'input appId here',
'secret' => 'input app secret here'
)
);
}
/**
* @return Facebook
*/
public function getFacebook() {
return $this->facebook;
}
}
有关更多帮助,请阅读:http://forge.typo3.org/projects/typo3v4-mvc/wiki/Dependency_Injection_(DI)关于initializeObject()和通过对象管理器创建原型对象的部分
Extbase注入非常简单. Here’s the actual implementation.但是,使用外部库不是.一旦你弄清楚如何加载库,你尝试过注入它吗?像这样:
/**
* @var Facebook
*/
protected $facebook;
/**
* inject the facebook
*
* @param Facebook facebook
* @return void
*/
public function injectFacebook(Facebook $facebook) {
$this->facebook = $facebook;
}
NOTE: You need the @param in the comment and you also need to clear your configuration cache after adding this code.
我不知道Facebook SDK API,但希望您可以使用默认构造函数实例化Facebook对象,然后使用setter方法添加参数.您可能想要创建一个加载Facebook PHP并设置基本参数的FacebookService类(单例).然后,您可以注入一个FacebookService,以便在需要时获取实际的Facebook对象.
