【发布时间】:2017-01-17 08:57:44
【问题描述】:
我在 Magento 2.1.3 中编译自定义模块时收到以下错误
Incorrect dependency in class NAMESPACE\MODULE\Block\SOMEBLOCK in /home/www/app/code/namespace/module/Block/Someblock.php
\Magento\Framework\Filesystem already exists in context object
简单来说,块代码是这样的;
namespace Namespace\Module\Block;
use Magento\Framework\App\Filesystem\DirectoryList;
class Someblock extends \Magento\Framework\View\Element\Template {
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\Filesystem $filesystem,
array $data = []
)
{
$this->_fileSystem = $filesystem;
parent::__construct($context, $data);
}
}
问题是代码在构造中注入\Magento\Framework\Filesystem,而它已经存在于继承的父类中。
对于非私有类,我知道我们可以在块类中调用它们;
$this->someclass
但是我们怎么称呼私有的呢?这个我试过了;
namespace Namespace\Module\Block;
use Magento\Framework\App\Filesystem\DirectoryList;
class Someblock extends \Magento\Framework\View\Element\Template {
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
//\Magento\Framework\Filesystem $filesystem,
array $data = []
)
{
//$this->_fileSystem = $filesystem;
parent::__construct($context, $data);
}
public function dosomething() {
$fileSystem = $this->_fileSystem;
}
}
但我得到 $_filesystem 未定义。
这里是父类\Magento\Framework\View\Element\Template的构造函数
public function __construct(Template\Context $context, array $data = [])
{
$this->validator = $context->getValidator();
$this->resolver = $context->getResolver();
$this->_filesystem = $context->getFilesystem();
$this->templateEnginePool = $context->getEnginePool();
$this->_storeManager = $context->getStoreManager();
$this->_appState = $context->getAppState();
$this->templateContext = $this;
$this->pageConfig = $context->getPageConfig();
parent::__construct($context, $data);
}
感谢您的反馈
【问题讨论】: