magento2 - Magento2 插件类路径未找到
问题描述
我为 Magento2 创建了一个插件来删除父类别路径,但出现如下错误:
Fatal error: Uncaught Error: Class
'Myweb\RemoveParentCategoryPathPlugin\Plugin\Category' not found in
/public_html/app/code/Myweb/RemoveParentCategoryPathPlugin/Plugin/RemoveParentCategoryPathPlugin.php:8 Stack trace: #0
/public_html/vendor/magento/framework/Interception/Interceptor.php(135):
Myweb\RemoveParentCategoryPathPlugin\Plugin\RemoveParentCategoryPathPlugin->aroundGetUrlPath(Object(Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator\Interceptor),
Object(Closure), Object(MGS\Mpanel\Model\Category\Interceptor)) #1
/public_html/vendor/magento/framework/Interception/Interceptor.php(153):
Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator\Interceptor->Magento\Framework\Interception\{closure}
(Object(MGS\Mpanel\Model\Category\Interceptor)) #2
/public_html/generated/code/Magento/CatalogUrlRewrite/Model/CategoryUrlPathGenerator/Interceptor.php(26):
Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator\Interceptor->___cal in /public_html/app/code/Myweb/RemoveParentCategoryPathPlugin/Plugin/RemoveParentCategoryPathPlugin.php on line 8
我的插件模块文件是这样的:
app/code/Myweb/RemoveParentCategoryPathPlugin/Plugin/RemoveParentCategoryPathPlugin.php
<?php
namespace Myweb\RemoveParentCategoryPathPlugin\Plugin;
class RemoveParentCategoryPathPlugin
{
public function aroundGetUrlPath($subject, $proceed, $category)
{
if (in_array($category->getParentId(), [Category::ROOT_CATEGORY_ID, Category::TREE_ROOT_ID])) {
return '';
}
$path = $category->getUrlPath();
if ($path !== null && !$category->dataHasChangedFor('url_key') && !$category->dataHasChangedFor('parent_id')) {
return $path;
}
$path = $category->getUrlKey();
if ($path === false) {
return $category->getUrlPath();
}
return $path;
}
}
app/code/Myweb/RemoveParentCategoryPathPlugin/etc/di.xml
内容是
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator">
<plugin name="Myweb_RemoveParentCategoryPathPlugin" type="Myweb\RemoveParentCategoryPathPlugin\Plugin\RemoveParentCategoryPathPlugin" />
</type>
</config>
app/code/Myweb/RemoveParentCategoryPathPlugin/etc/module.xml
内容是
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Myweb_RemoveParentCategoryPathPlugin" setup_version="1.0.5" />
</config>
app/code/Myweb/RemoveParentCategoryPathPlugin/registration.php
内容是
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Myweb_RemoveParentCategoryPathPlugin',
__DIR__
);
我正在努力寻找,我错过了什么。
解决方案
在您的插件类 RemoveParentCategoryPathPlugin 中,缺少 Category 类。在您的文件中添加这一行“使用 Magento\Catalog\Model\Category”。
推荐阅读
- r - 变量 cyl 作为原子向量
- javascript - 从字符串列表创建动态 SVG
- inno-setup - 我可以在 [Setup] 部分使用什么替代方法来代替 Inno Setup 中的自定义消息?
- google-visualization - 谷歌可视化条形格式
- react-native - 反应导航 v5 从抽屉自定义视图导航
- java - 澳大利亚/墨尔本时区的 Java 8 中的日期时间夏令时问题
- c# - 如何使用反射将 IList 动态转换为任意数据类型的数组
- java - Java中由主线程创建的两个线程之间的通信
- elasticsearch - 丢失所有弹性搜索索引
- java - AWS S3 上传公开可读的对象