首页 > 解决方案 > 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__
);

我正在努力寻找,我错过了什么。

标签: magento2

解决方案


在您的插件类 RemoveParentCategoryPathPlugin 中,缺少 Category 类。在您的文件中添加这一行“使用 Magento\Catalog\Model\Category”。


推荐阅读