我正在使用 Extbase 1.4 开发一个 TYPO3 4.6 扩展,并且我试图包含一个外部库。图书馆,在我的例子中是 facebook PHP SDK , 在 $_EXTKEY/Resources/PHP/facebook-php-sdk/facebook.php
下.我希望库在我需要的地方自动加载并自动注入(inject)(Dependecy Injection)。
我在网上找到的一些评论建议应该包含带有 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 函数自动注入(inject)库呢?
编辑:
点赞@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',
);
?>
它似乎找到并包含正确的文件。但是当我尝试使用用户依赖注入(inject)(如 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 参数。
编辑2:
在 @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)关于 的部分初始化对象() 和 通过对象管理器创建原型(prototype)对象
最佳答案
首先在扩展根文件夹中创建 ext_autoload.php
并添加您的代码,它包含以键为类名的一维数组(类名必须以扩展键为前缀)和值作为文件的路径。
确保清除您的网站
<?php
$extensionPath = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('rent_system');
return array(
'rent_system_TCPDF' => $extensionPath.'Resources/Private/PHP/tcpdf/tcpdf.php',
);
?>
在 Controller 文件中
$pdf = $this->objectManager->create('rent_system_TCPDF');
关于dependency-injection - 如何在 TYPO3 Extbase 扩展中包含或自动加载外部库? + 依赖注入(inject)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10604377/