Shopware 6插件开发中Endroid QR Code类加载失败求助
Shopware 6插件引入Endroid QR Code库时ClassNotFoundError问题
问题概述
开发Shopware 6插件时,使用Endroid QR Code库出现ClassNotFoundError: Attempted to load class "Builder" from namespace "Endroid\QrCode\Builder"错误。已确认命名空间正确,通过Composer安装了依赖,代码与Packagist示例一致,但错误依旧。尝试使用bacon/bacon-qr-code库时,也遇到类似的ImageRenderer类加载问题。
环境配置:
- Docker v4.34.4
- Symfony v6.6.6.1
- PHP v8.3
2024年10月13日更新:缓存清理与自动加载日志
执行以下命令后的日志:
www-data@741042a1609d:~/html$ php bin/console cache:clear // Clearing the cache for the dev environment with debug true // Warming up cache... // Warming up optional cache... // Removing old build and cache directory... // Finished [OK] Cache for the "dev" environment (debug=true) was successfully cleared. www-data@741042a1609d:~/html$ cd custom/plugins/ShrQrProGen/ www-data@741042a1609d:~/html/custom/plugins/ShrQrProGen$ composer dump-autoload -o Deprecation Notice: Using ${var} in strings is deprecated, use {$var} instead in phar:///usr/local/bin/composer/src/Composer/Autoload/AutoloadGenerator.php:879 Deprecation Notice: Using ${var} in strings is deprecated, use {$var} instead in phar:///usr/local/bin/composer/src/Composer/Autoload/AutoloadGenerator.php:884 Generating optimized autoload files Generated optimized autoload files containing 9983 classes www-data@741042a1609d:~/html/custom/plugins/ShrQrProGen$ tail -f ../../../var/log/dev.log [2024-10-13T18:36:35.730919+00:00] app.INFO: #9 Rule detection: Alle Kunden der Standard-Kundengruppe with priority 1 (id: 28caae75a5624f0d985abd0eb32aa160) [] [] [2024-10-13T18:36:35.732180+00:00] php.INFO: User Deprecated: Since shopware/core : The tag `shopware.payment.method.sync` is deprecated for service Shopware\Core\Checkout\Payment\Cart\PaymentHandler\PrePayment and will be removed in 6.7.0. Use `shopware.payment.handler` instead. {"exception":"[object] (ErrorException(code: 0): User Deprecated: Since shopware/core : The tag `shopware.payment.method.sync` is deprecated for service Shopware\\Core\\Checkout\\Payment\\Cart\\PaymentHandler\\PrePayment and will be removed in 6.7.0. Use `shopware.payment.handler` instead. at /var/www/html/vendor/shopware/core/Checkout/Payment/Cart/PaymentHandler/PaymentHandlerRegistry.php:48)"} [] [2024-10-13T18:36:35.732326+00:00] php.INFO: User Deprecated: Since shopware/core : The tag `shopware.payment.method.sync` is deprecated for service Shopware\Core\Checkout\Payment\Cart\PaymentHandler\DebitPayment and will be removed in 6.7.0. Use `shopware.payment.handler` instead. {"exception":"[object] (ErrorException(code: 0): User Deprecated: Since shopware/core : The tag `shopware.payment.method.sync` is deprecated for service Shopware\\Core\\Checkout\\Payment\\Cart\\PaymentHandler\\DebitPayment and will be removed in 6.7.0. Use `shopware.payment.handler` instead. at /var/www/html/vendor/shopware/core/Checkout/Payment/Cart/PaymentHandler/PaymentHandlerRegistry.php:48)"} [] [2024-10-13T18:36:35.732418+00:00] php.INFO: User Deprecated: Since shopware/core : The tag `shopware.payment.method.sync` is deprecated for service Shopware\Core\Checkout\Payment\Cart\PaymentHandler\CashPayment and will be removed in 6.7.0. Use `shopware.payment.handler` instead. {"exception":"[object] (ErrorException(code: 0): User Deprecated: Since shopware/core : The tag `shopware.payment.method.sync` is deprecated for service Shopware\\Core\\Checkout\\Payment\\Cart\\PaymentHandler\\CashPayment and will be removed in 6.7.0. Use `shopware.payment.handler` instead. at /var/www/html/vendor/shopware/core/Checkout/Payment/Cart/PaymentHandler/PaymentHandlerRegistry.php:48)"} [] [2024-10-13T18:36:35.732509+00:00] php.INFO: User Deprecated: Since shopware/core : The tag `shopware.payment.method.sync` is deprecated for service Shopware\Core\Checkout\Payment\Cart\PaymentHandler\InvoicePayment and will be removed in 6.7.0. Use `shopware.payment.handler` instead. {"exception":"[object] (ErrorException(code: 0): User Deprecated: Since shopware/core : The tag `shopware.payment.method.sync` is deprecated for service Shopware\\Core\\Checkout\\Payment\\Cart\\PaymentHandler\\InvoicePayment and will be removed in 6.7.0. Use `shopware.payment.handler` instead. at /var/www/html/vendor/shopware/core/Checkout/Payment/Cart/PaymentHandler/PaymentHandlerRegistry.php:48)"} [] [2024-10-13T18:36:35.732590+00:00] php.INFO: User Deprecated: Since shopware/core : The tag `shopware.payment.method.sync` is deprecated for service Shopware\Core\Checkout\Payment\Cart\PaymentHandler\DefaultPayment and will be removed in 6.7.0. Use `shopware.payment.handler` instead. {"exception":"[object] (ErrorException(code: 0): User Deprecated: Since shopware/core : The tag `shopware.payment.method.sync` is deprecated for service Shopware\\Core\\Checkout\\Payment\\Cart\\PaymentHandler\\DefaultPayment and will be removed in 6.7.0. Use `shopware.payment.handler` instead. at /var/www/html/vendor/shopware/core/Checkout/Payment/Cart/PaymentHandler/PaymentHandlerRegistry.php:48)"} [] [2024-10-13T18:36:35.869849+00:00] php.INFO: User Deprecated: Since shopware/core : Method "Shopware\Storefront\Page\Product\ProductPage::setReviews()" is deprecated and will be removed in v6.7.0.0. {"exception":"[object] (ErrorException(code: 0): User Deprecated: Since shopware/core : Method \"Shopware\\Storefront\\Page\\Product\\ProductPage::setReviews()\" is deprecated and will be removed in v6.7.0.0. at /var/www/html/vendor/shopware/core/Framework/Feature.php:242)"} [] [2024-10-13T18:36:35.869992+00:00] php.INFO: User Deprecated: Since shopware/core : Method "Shopware\Storefront\Page\Product\ProductPage::setCrossSellings()" is deprecated and will be removed in v6.7.0.0. {"exception":"[object] (ErrorException(code: 0): User Deprecated: Since shopware/core : Method \"Shopware\\Storefront\\Page\\Product\\ProductPage::setCrossSellings()\" is deprecated and will be removed in v6.7.0.0. at /var/www/html/vendor/shopware/core/Framework/Feature.php:242)"} [] [2024-10-13T18:36:35.870170+00:00] app.INFO: QrProGenSubscriber: onProductPageLoaded [] [] [2024-10-13T18:36:36.066754+00:00] request.CRITICAL: Uncaught PHP Exception Symfony\Component\ErrorHandler\Error\ClassNotFoundError: "Attempted to load class \"Builder\" from namespace \"Endroid\QrCode\Builder\". Did you forget a \"use\" statement for e.g. \"Lcobucci\JWT\Token\Builder\" or \"Lcobucci\JWT\Builder\"?" at QrProGenSubscriber.php line 105 {"exception":"[object] (Symfony\\Component\\ErrorHandler\\Error\\ClassNotFoundError(code: 0): Attempted to load class \"Builder\" from namespace \"Endroid\\QrCode\\Builder\".\nDid you forget a \"use\" statement for e.g. \"Lcobucci\\JWT\\Token\\Builder\" or \"Lcobucci\\JWT\\Builder\"? at /var/www/html/custom/plugins/ShrQrProGen/src/Subscriber/QrProGenSubscriber.php:105)"} []
2024年10月14日更新:相关文件内容
composer.json
{ .. "require": { "shopware/core": "6.6.*", "endroid/qr-code": "^5.1" }, "autoload": { "psr-4": { "Shr\QrProGen": "src/" } }, .. }
ShrQrProGen.php
<?php declare(strict_types=1); namespace Shr\QrProGen; use Shopware\Core\Framework\Plugin; class ShrQrProGen extends Plugin { public function boot(): void { parent::boot(); $autoload = $this->getPath() . '/vendor/autoload.php'; if (file_exists($autoload)) { require_once $autoload; } } }
解决方法
1. 移除插件内手动加载autoload的代码
插件boot方法里手动加载插件目录下的vendor/autoload.php会干扰Shopware全局的Composer自动加载机制,直接删除这段代码:
<?php declare(strict_types=1); namespace Shr\QrProGen; use Shopware\Core\Framework\Plugin; class ShrQrProGen extends Plugin { // 继承父类默认实现即可,无需额外代码 }
2. 正确通过Shopware根目录安装依赖
Shopware 6插件的依赖必须通过项目根目录的Composer管理,不能在插件目录单独安装:
- 进入项目根目录:
cd /var/www/html/ - 安装依赖:
composer require endroid/qr-code "^5.1" - 优化自动加载:
composer dump-autoload -o
3. 清理缓存并重新安装插件
- 清理Shopware缓存:
php bin/console cache:clear - 刷新并重新安装插件:
php bin/console plugin:refresh && php bin/console plugin:install ShrQrProGen -a
4. 确认代码中的命名空间引用
在使用Builder类的文件(如QrProGenSubscriber.php)顶部添加正确的use语句:
use Endroid\QrCode\Builder\Builder;
问题根源
Shopware 6依赖全局Composer自动加载器,插件目录内单独安装的依赖不会被全局识别。手动加载插件的autoload文件会引发类加载冲突,导致依赖类无法被正确找到。必须通过根目录Composer统一管理所有依赖。
内容的提问来源于stack exchange,提问作者Premox
相关产品推荐
相关产品推荐

