【问题标题】:Magento2: Calling a protected class, compile error: Incorrect dependencyMagento2:调用受保护的类,编译错误:依赖不正确
【发布时间】: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);
}

感谢您的反馈

【问题讨论】:

    标签: magento magento2


    【解决方案1】:

    知道了,答案就在眼前!父构造函数是;

    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);
    }
    

    变量是;

    $this->_filesystem 
    

    不是

    $this->_fileSystem 
    

    我有的。

    【讨论】:

      【解决方案2】:

      你需要试试这个,

      namespace Namespace\Module\Block;
      
      use Magento\Framework\App\Filesystem\DirectoryList;
      
      class Someblock extends \Magento\Framework\View\Element\Template 
      {
          public $myfileobj;
          public function __construct(
               \Magento\Framework\View\Element\Template\Context $context,
               array $data = []
          )
          {
              parent::__construct($context, $data);
          }
      
          public function dosomething() 
          {
              $this->myfileobj = $this->getFilesystem();
          }
      
      }
      

      您也可以在其他地方使用 $this->myfileobj。

      【讨论】:

      • 这也是我尝试过的——它会产生错误;错误过滤模板:注意:未定义变量:文件系统
      • $fileSystem 只使用了 dosomething() 这个函数,如果你需要在另一个函数中使用它,那么你需要使用另一个变量。
      • 抱歉打错了,这个;错误过滤模板:注意:未定义的属性:Namespace\Module\Block\Someblock::$fileSystem 我在函数中直接使用 $this->fileSystem->getDirectoryWrite(DirectoryList::VAR_DIR)->getAbsolutePath() 调用它。 'instagram_timer';
      • 谢谢,这是父构造函数和我的模块(doh)之间的大小写不匹配
      • 如果以上答案对您有帮助,请接受它将帮助社区中的其他人。
      猜你喜欢
      • 2011-12-26
      • 1970-01-01
      • 1970-01-01
      • 2013-01-06
      • 1970-01-01
      • 1970-01-01
      • 2017-10-27
      • 2015-10-03
      • 2019-12-05
      相关资源
      最近更新 更多