业务需求创造了一个bug,需要修复,经过老长一段时间的折腾,定位出原因了,解决方案,重写核心代码,放开限制。
3下5除2开开心心地进行模块重写,启用模块,升级,并编译,结果报了Fatal error: Uncaught Error: Undefined constant和PHP Fatal error: Class 'Multiline' not found,如下
sudo bin/magento setup:di:compile
Compilation was started.
Repositories code generation... 1/7 [====>-----------------------] 14% 2 secs 101.0 MiBPHP Fatal error: Uncaught Error: Class 'Sna\Eav\Model\Attribute\Data\Magento\Eav\Model\Attribute\Data\Multiline' not found in /var/www/html/m2.34-jp/app/code/Sna/Eav/Model/Attribute/Data/Multiline.php:15
Stack trace:
#0 /var/www/html/m2.34-jp/vendor/composer/ClassLoader.php(444): include()
#1 /var/www/html/m2.34-jp/vendor/composer/ClassLoader.php(322): Composer\Autoload\includeFile()
#2 [internal function]: Composer\Autoload\ClassLoader->loadClass()
#3 [internal function]: spl_autoload_call()
#4 /var/www/html/m2.34-jp/setup/src/Magento/Setup/Module/Di/Code/Reader/ClassesScanner.php(133): class_exists()
#5 /var/www/html/m2.34-jp/setup/src/Magento/Setup/Module/Di/Code/Reader/ClassesScanner.php(117): Magento\Setup\Module\Di\Code\Reader\ClassesScanner->includeClasses()
#6 /var/www/html/m2.34-jp/setup/src/Magento/Setup/Module/Di/Code/Reader/ClassesScanner.php(88): Magento\Setup\Module\Di\Code\Reader\ClassesScanner->extract()
#7 /var/www/html/m2.34-jp/setup/src/Magento/Setup/Module/Di/App/Task/Operation/RepositoryGenerator.php(61 in /var/www/html/m2.34-jp/app/code/Sna/Eav/Model/Attribute/Data/Multiline.php on line 15
找来找去终于知道了原因,开头的class Multiline extends Magento\Eav\Model\Attribute\Data\Multiline的写法要改一下,extends后面的Magento要以\开头,即是\Magento,完整如下:
再次编译,已经不报相同的错误了,但却报了另外一个错误,如下
namespace \Silk\Eav\Model\Attribute\Data;
// use Magento\Framework\App\RequestInterface;
/**
* EAV Entity Attribute Multiply line Data Model
*
* @author Magento Core Team <[email protected]>
*/
class Multiline extends \Magento\Eav\Model\Attribute\Data\Multiline
{
/*..省略...*/
}
这,,入坑刚起来又直接掉坑,好吧,再细看一下文件代码的报错说明,namespace里面以\开头了,尝试去除,再重新编译,可算正常了。正常的代码如下:
#namespace \Silk\Eav\Model\Attribute\Data;
#不能以\开头,编译时会出错
namespace Silk\Eav\Model\Attribute\Data;
// use Magento\Framework\App\RequestInterface;
/**
* EAV Entity Attribute Multiply line Data Model
*
* @author Magento Core Team <[email protected]>
*/
class Multiline extends \Magento\Eav\Model\Attribute\Data\Multiline
{
/*..省略...*/
}
总结:Magneto2模块在功能重写时,namespace不要以\开头,而重写继承的模块在extends后面的Magento模块,则要以\开头,这样进行重写时,才能找到正常的class,编译才会正常