php - Magento 2 - 设置 :di:compile 中不存在类

标签 php magento namespaces magento2

我对 Magento 2 有一些问题。我制作了自己的模块,它看起来工作正常,但是当我安装了我的模块并制作 php bin/magento setup:di:compile 时有这个错误:

[ReflectionException] Class \MyInstaller does not exist



它是由 setup/src/Magento/Setup/Module/PhpScanner.php 抛出的

MyInstaller 类存在,但是我更改了类文件的路径。原来有路径/my-libraries/MyInstaller.php ,但是当我在本地测试时,出现了如上的错误:

[ReflectionException] Class \MyInstaller does not exist



当我将文件夹名称更改为 mylibraries 时,在 localhost 上它工作正常。但是,当我在外部服务器上安装模块时,我有 ReflectionException .当我将文件夹名称更改为原始名称 - “my-libraries”时,它不会显示。正如我所观察到的,Magento 仍然调用旧路径。但是,我尝试运行 Magento 2 的新实例,但仍然出现此错误。

我试图通过以下方式清除缓存:
bin/magento cache:clean
bin/magento cache:flush

我试图删除 /var文件夹也是。启用我的模块后,我使:
php bin/magento setup:upgrade


php bin/magento setup:static-content:deploy

你能告诉我,我怎样才能强制 Magento 使用文件的新路径?或者我应该更改服务器上的一些缓存设置?

我补充说,MyInstaller 类中没有命名空间,因为我在另一个模块中使用了相同的文件。但是,如果在本地主机上没有错误,我认为它们是没有必要的。

有 MyInstaller 类代码:
<?php

if (!class_exists('MyInstaller', false)) {
class MyInstaller implements MyInterface
{
    private $translations;
    private $sliderEnabled = true;
    private $pages = array();

    public function __construct($sliderEnabled = true, array $translations = array())
    {
        $this->sliderEnabled = $sliderEnabled;
        $this->setTranslations($translations);
    }

    public function setTranslations(array $translations = array())
    {
        $this->translations = $translations;

    }

    public function addPages(array $pages = array())
    {
        $this->pages = array_values($pages);
    }

    public function renderInstallerSteps()
    {
        if (!$this->sliderEnabled || empty($this->pages) || !is_array($this->pages)) {
            return '';
        }

        $requirements = $this->checkRequirements();
        $params = array(
            'requirements' => $requirements,
            'translations' => $this->translations
        );
        $maxSteps = 0;
        $data = array(
            'steps' => array()
        );
        foreach ($this->pages as $page) {
            $page = (int)$page;
            if ($page > 0) {
                $step = $this->loadStep($page, $params);
                $data['steps'][$page] = $step;
                $maxSteps++;
            }
        }

        if ($maxSteps === 0) {
            return '';
        }
        $data['maxSteps'] = $maxSteps;

        return $this->loadTemplate('installer', $data);
    }

    private function loadStep($number, $params = null)
    {
        $step = $this->loadTemplate('step' . $number, $params);
        $step = $this->removeNewLines($step);
        return $step;
    }

    private function removeNewLines($string)
    {
        return trim(str_replace(PHP_EOL, ' ', $string));
    }

    private function loadTemplate($view, $data = null)
    {
        extract(array("content" => $data));
        ob_start();
        $viewFile = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'theme' . DIRECTORY_SEPARATOR . "$view.tpl.php";

        if (file_exists($viewFile)) {
            include $viewFile;
        } else {
            throw new Exception('View not exist in ' . get_class($this));
        }
        $content = ob_get_clean();
        return $content;
    }

    private function checkRequirements()
    {
        $data = array(
            'php' => array(
                'test' => (version_compare(PHP_VERSION, '5.2.0') > 0),
                'label' => $this->translations['php_version']
            ),
            'curl' => array(
                'test' => function_exists('curl_version'),
                'label' => $this->translations['curl_enabled']
            ),
            'soap' => array(
                'test' => class_exists('SoapClient'),
                'label' => $this->translations['soap_enabled']
            )
        );
        return $data;
    }
}

}

对不起,如果描述困惑,这是我在这里的第一篇文章。

最佳答案

Magento2 依赖命名空间来分隔和定位模块和类。

所有文件都需要一个声明的命名空间,如下所示:

<?php
    namespace Vendor\MyModule\Setup;

    class MyInstaller implements ... {}

编辑 - 看到评论后

还要检查是否区分大小写 - UNIX 系统将 This 和 this 视为两个单独的文件,而 Windows 会将其视为一个文件。

关于php - Magento 2 - 设置 :di:compile 中不存在类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50098386/

相关文章:

php - 基于MySQL表通过PHP创建一条空白记录

php - 在 Windows 上的 WAMP 服务器 (3.0.6 x64) 中安装 php_trader.dll

php - 使用 password_hash() 和 password_verify() 的问题

php - magento 数据库 sql 不工作

magento - 错误magento 1.9-使用未定义的常数tmp-假定为 'tmp'

c# - 导入整个命名空间与在 C# 中使用别名之间的性能?

php - 单页上的 Concrete5 Express 列表的命名空间问题

javascript - 使用 jQuery/javascript 仅筛选出我需要的列的 JSON

layout - 如何在产品 View 页面显示类别自定义设计布局?

c# - 如何让命名空间别名 operator::在 C# 下工作?