php - Magento 2 - setup:di:compile 上不存在类
问题描述
我对 Magento 2 有一些问题。我制作了自己的模块,它看起来工作正常,但是当我安装了我的模块并制作php bin/magento setup:di:compile
时出现了这个错误:
[ReflectionException] 类 \MyInstaller 不存在
它被抛出setup/src/Magento/Setup/Module/PhpScanner.php
MyInstaller 类存在,但是我更改了类文件的路径。本来是有 path/my-libraries/MyInstaller.php
的,但是当我在本地测试时,出现了如上的错误:
[ReflectionException] 类 \MyInstaller 不存在
当我将文件夹名称更改为 mylibraries 时,在 localhost 上它工作正常。但是,当我在外部服务器上安装模块时,我有ReflectionException
. 当我将文件夹名称更改为原始名称 - “我的库”时,它不会显示。正如我所观察到的,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 类中没有命名空间,因为我在其他模块中使用了相同的文件。但是,如果在 localhost 上没有错误,我认为它们是不必要的。
有 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 将其视为一个。
推荐阅读
- django - 为什么要验证_
触发3次?Django 休息框架 - android - 广播接收器在android oreo和pie的后台不工作
- javascript - 在两个菜单中使用 jQuery pushy js 时遇到问题
- java - 在测试我在某些手机中使用共享首选项的应用时,它不支持多个共享首选项?这是什么原因?
- chart.js - 有没有办法创建一个只有一个轴的图表?
- c# - ASP.NET Core CORS 组合策略;控制器与中间件配置中的 enablecors
- lambda - 比较器:如何使用 nullsFirst 和流 api?
- android - 在 Espresso 测试脚本中输入大文本的问题
- javascript - 在 React JS 中导入数组函数后无法检索我的电影数据
- google-maps - 如何访问谷歌地图中某个地点的事件数据?