0
我想在Pimcore中使用依赖注入。幸运的是,自Pimcore 4.x以来,这是可能的。Pimcore和依赖注入
但我必须承认,我无法让它工作。我读了这些网站:DI,DI,DI
以及Pimcore使用它的php-di文档。
现在我有一个插件,一个简单的搜索,我想注入到我的控制器之一。
的插件结构如下:
plugins
- MyPlugin
- lib
- MyPlugin
- Models
Search.php
- static
- plugin.xml
我想里面Search.php
类注入到我的控制器。
# plugins/MyPlugin/lib/MyPlugin/Models/Search.php
class MyPlugin_Models_Search
类本身没有命名空间,但是有一个plugin.xml中
# plugins/MyPlugin/plugin.xml
<pluginNamespace>MyPlugin</pluginNamespace>
定义我复制了di.example.php
到website/config/di.php
,它被称为,我veryfied这一点。
我的定义是这样的:
# website/config/di.php
<?php
return [
'MyPlugin' => DI\object()->property('myPlugin', DI\get('MyPlugin_Models_Search')),
# 'myPlugin' => \DI\object()->constructor(DI\get('MyPlugin_Models_Search'))
# 'myPlugin' => new MyPlugin_Models_Search() // crashes everything :)
# 'MyPlugin_Models_Search' => new MyPlugin_Models_Search()
# many more ...
];
我的控制器:
# website/controllers/FooController.php
<?php
use Website\Controller\Action;
class FooController extends Action
{
public $myPlugin;
# this won't work: Declaration of FooController::__construct() must be
# compatible with Zend_Controller_Action_Interface::__construct(
# Zend_Controller_Request_Abstract $request, Zend_Controller_Response_
# Abstract $response, array $invokeArgs = Array)
public function __construct($plugin) {
$this->myPlugin = $plugin;
}
public function FooAction() { var_dump($this->myPlugin); }
}
我甚至想直接在pimcore/lib/Pimcore.php
设置定义function getDiContainer()
,但没有成功。
任何想法?谢谢你,祝你有美好的一天!
btw:从来没有必要修改用于安装插件的DI文件。 – campino2k
你可以看看其他公共插件,如“Member”https://github.com/pimcore-extensions/member/blob/master/plugin.xml – campino2k