shopware6-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseShopware 6 Best Practices
Shopware 6 开发最佳实践
Comprehensive best practices guide for Shopware 6.6+ development, designed for AI agents and LLMs helping agency developers. Contains 77 rules across 22 categories, prioritized by impact to guide automated refactoring and code generation.
这份Shopware 6.6+开发最佳实践指南内容全面,专为协助代理开发者的AI Agent与大语言模型设计。包含22个分类下的77条规则,按影响优先级排序,可指导自动化重构与代码生成。
When to Apply
适用场景
Reference these guidelines when:
- Developing custom Shopware 6 plugins
- Creating or modifying Store API or Admin API endpoints
- Working with the Data Abstraction Layer (DAL)
- Implementing event subscribers and decorators
- Configuring message queue handlers
- Writing database migrations
- Optimizing performance (caching, Elasticsearch)
- Implementing security measures
- Writing unit and integration tests
- Customizing storefront templates and JavaScript
- Building administration modules and components
- Creating Shopware apps with webhooks and actions
- Implementing payment or shipping integrations
- Building CMS elements and blocks
- Creating CLI commands for automation
- Setting up multi-channel or B2B shops
- Configuring development environments and CI/CD
在以下场景中可参考本指南:
- 开发自定义Shopware 6插件
- 创建或修改Store API或Admin API端点
- 操作数据抽象层(DAL)
- 实现事件订阅器与装饰器
- 配置消息队列处理器
- 编写数据库迁移脚本
- 优化性能(缓存、Elasticsearch)
- 实施安全措施
- 编写单元测试与集成测试
- 定制店面模板与JavaScript
- 构建管理端模块与组件
- 通过Webhook与动作创建Shopware应用
- 实现支付或物流集成
- 构建CMS元素与区块
- 创建用于自动化的CLI命令
- 搭建多渠道或B2B店铺
- 配置开发环境与CI/CD
Rule Categories by Priority
按优先级划分的规则分类
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Plugin Architecture | CRITICAL | |
| 2 | Customization & Extension | CRITICAL | |
| 3 | Performance & Caching | CRITICAL | |
| 4 | Security | CRITICAL | |
| 5 | Data Abstraction Layer | HIGH | |
| 6 | API Development | HIGH | |
| 7 | Testing | HIGH | |
| 8 | Event System | MEDIUM-HIGH | |
| 9 | Database & Migrations | MEDIUM-HIGH | |
| 10 | Message Queue | MEDIUM | |
| 11 | Dependency Injection | MEDIUM | |
| 12 | Logging | MEDIUM | |
| 13 | Configuration | MEDIUM | |
| 14 | Scheduled Tasks | MEDIUM | |
| 15 | Storefront Development | HIGH | |
| 16 | Administration Development | HIGH | |
| 17 | App System | HIGH | |
| 18 | Integration Patterns | HIGH | |
| 19 | CLI Commands | MEDIUM | |
| 20 | Multi-Channel & B2B | MEDIUM-HIGH | |
| 21 | DevOps & Tooling | MEDIUM | |
| 22 | Common Patterns | HIGH | |
| 优先级 | 分类 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 插件架构 | 关键 | |
| 2 | 定制与扩展 | 关键 | |
| 3 | 性能与缓存 | 关键 | |
| 4 | 安全 | 关键 | |
| 5 | 数据抽象层 | 高 | |
| 6 | API开发 | 高 | |
| 7 | 测试 | 高 | |
| 8 | 事件系统 | 中高 | |
| 9 | 数据库与迁移 | 中高 | |
| 10 | 消息队列 | 中 | |
| 11 | 依赖注入 | 中 | |
| 12 | 日志 | 中 | |
| 13 | 配置 | 中 | |
| 14 | 定时任务 | 中 | |
| 15 | 店面开发 | 高 | |
| 16 | 管理端开发 | 高 | |
| 17 | 应用系统 | 高 | |
| 18 | 集成模式 | 高 | |
| 19 | CLI命令 | 中 | |
| 20 | 多渠道与B2B | 中高 | |
| 21 | DevOps与工具 | 中 | |
| 22 | 通用模式 | 高 | |
Quick Reference
快速参考
1. Plugin Architecture (CRITICAL)
1. 插件架构(关键)
- - Follow proper plugin directory structure and composer.json
plugin-structure - - Register services correctly with proper tags
plugin-services
- - 遵循规范的插件目录结构与composer.json配置
plugin-structure - - 正确注册服务并添加合适的标签
plugin-services
2. Customization & Extension (CRITICAL)
2. 定制与扩展(关键)
- - Use decorator pattern for upgrade-safe service customization
custom-decorator-pattern - - Implement event subscribers correctly with proper priorities
custom-event-subscribers
- - 使用装饰器模式实现可安全升级的服务定制
custom-decorator-pattern - - 正确实现事件订阅器并设置合适的优先级
custom-event-subscribers
3. Performance & Caching (CRITICAL)
3. 性能与缓存(关键)
- - Configure HTTP cache correctly with proper invalidation
perf-http-cache - - Optimize DAL queries to prevent N+1 problems
perf-dal-optimization - - Use Elasticsearch correctly for large catalogs
perf-elasticsearch
- - 正确配置HTTP缓存及失效策略
perf-http-cache - - 优化DAL查询以避免N+1问题
perf-dal-optimization - - 针对大型商品目录正确使用Elasticsearch
perf-elasticsearch
4. Security (CRITICAL)
4. 安全(关键)
- - Validate all inputs with RequestDataBag and validators
security-input-validation - - Implement proper route authentication
security-authentication - - Use ACL for permission checks
security-authorization - - Implement CSRF protection for storefront
security-csrf-protection - - Use DAL or parameterized queries
security-sql-injection
- - 使用RequestDataBag与验证器校验所有输入
security-input-validation - - 实现规范的路由认证
security-authentication - - 使用ACL进行权限校验
security-authorization - - 为店面实现CSRF防护
security-csrf-protection - - 使用DAL或参数化查询
security-sql-injection
5. Data Abstraction Layer (HIGH)
5. 数据抽象层(高)
- - Use Criteria objects correctly with filters and pagination
dal-criteria-usage - - Load associations explicitly to avoid N+1 queries
dal-associations - - Use batch operations and proper sync patterns
dal-write-operations - - Extend entities without modifying core
dal-entity-extensions - - Create custom entity definitions properly
dal-custom-entities
- - 正确使用Criteria对象实现过滤与分页
dal-criteria-usage - - 显式加载关联以避免N+1查询
dal-associations - - 使用批量操作与规范的同步模式
dal-write-operations - - 在不修改核心代码的前提下扩展实体
dal-entity-extensions - - 正确创建自定义实体定义
dal-custom-entities
6. API Development (HIGH)
6. API开发(高)
- - Create decoratable Store API routes
api-store-api-routes - - Create Admin API endpoints with proper ACL
api-admin-api-routes - - Handle responses and errors consistently
api-response-handling - - Configure rate limiting for protection
api-rate-limiting - - Version APIs for backwards compatibility
api-versioning
- - 创建可被装饰的Store API路由
api-store-api-routes - - 实现带有规范ACL的Admin API端点
api-admin-api-routes - - 统一处理响应与错误
api-response-handling - - 配置限流以提供防护
api-rate-limiting - - 为API添加版本控制以保证向后兼容
api-versioning
7. Testing (HIGH)
7. 测试(高)
- - Write unit tests with proper mocking
test-unit-tests - - Use IntegrationTestBehaviour correctly
test-integration-tests - - Test Store API routes with HTTP tests
test-store-api-tests - - Create reusable test fixtures
test-fixtures
- - 编写带有合理模拟的单元测试
test-unit-tests - - 正确使用IntegrationTestBehaviour
test-integration-tests - - 通过HTTP测试验证Store API路由
test-store-api-tests - - 创建可复用的测试夹具
test-fixtures
8. Event System (MEDIUM-HIGH)
8. 事件系统(中高)
- - Create Flow Builder compatible events
event-business-events - - Implement configurable flow actions
event-flow-actions
- - 创建兼容Flow Builder的事件
event-business-events - - 实现可配置的流动作
event-flow-actions
9. Database & Migrations (MEDIUM-HIGH)
9. 数据库与迁移(中高)
- - Implement safe database migrations
db-migrations
- - 实现安全的数据库迁移
db-migrations
10. Message Queue (MEDIUM)
10. 消息队列(中)
- - Implement async message handling
queue-message-handlers - - Configure workers for production
queue-worker-config - - Separate low-priority background tasks
queue-low-priority
- - 实现异步消息处理
queue-message-handlers - - 为生产环境配置工作进程
queue-worker-config - - 分离低优先级的后台任务
queue-low-priority
11. Dependency Injection (MEDIUM)
11. 依赖注入(中)
- - Use Symfony DI correctly
di-service-container
- - 正确使用Symfony依赖注入容器
di-service-container
12. Logging (MEDIUM)
12. 日志(中)
- - Implement structured logging
logging-best-practices
- - 实现结构化日志
logging-best-practices
13. Configuration (MEDIUM)
13. 配置(中)
- - Implement plugin configuration correctly
config-plugin-settings
- - 正确实现插件配置
config-plugin-settings
14. Scheduled Tasks (MEDIUM)
14. 定时任务(中)
- - Implement reliable scheduled tasks
scheduled-tasks
- - 实现可靠的定时任务
scheduled-tasks
15. Storefront Development (HIGH)
15. 店面开发(高)
- - Extend StorefrontController with page loaders
storefront-controller-pattern - - Template inheritance with sw_extends and blocks
storefront-twig-extension - - JavaScript plugin registration and lifecycle
storefront-js-plugins - - Theme structure, inheritance, and configuration
storefront-themes - - SCSS variables and responsive mixins
storefront-scss-variables - - AJAX requests and Store API calls from JS
storefront-http-client
- - 扩展StorefrontController并使用页面加载器
storefront-controller-pattern - - 使用sw_extends与区块实现模板继承
storefront-twig-extension - - JavaScript插件的注册与生命周期管理
storefront-js-plugins - - 主题结构、继承与配置
storefront-themes - - SCSS变量与响应式混合宏
storefront-scss-variables - - 从JS发起AJAX请求与Store API调用
storefront-http-client
16. Administration Development (HIGH)
16. 管理端开发(高)
- - Module registration, routes, and navigation
admin-module-structure - - Vue component patterns and templates
admin-components - - Repository factory and Criteria API in admin
admin-data-handling - - ACL-based UI visibility and actions
admin-acl-permissions - - Mixins, directives, and extensibility
admin-mixins-composables - - App iframe modules and Extension SDK
admin-extension-api
- - 模块注册、路由与导航
admin-module-structure - - Vue组件模式与模板
admin-components - - 管理端中的仓库工厂与Criteria API
admin-data-handling - - 基于ACL的UI可见性与动作控制
admin-acl-permissions - - 混入、指令与可扩展性
admin-mixins-composables - - 应用iframe模块与Extension SDK
admin-extension-api
17. App System (HIGH)
17. 应用系统(高)
- - Complete manifest.xml configuration
app-manifest - - Webhook handling and signature verification
app-webhooks - - Action buttons with notifications, modals, redirects
app-custom-actions - - App payment handlers for pay/finalize/capture/refund
app-payment-methods - - Custom field definitions via manifest
app-custom-fields - - Twig-based app scripts and hook points
app-scripts
- - 完整的manifest.xml配置
app-manifest - - Webhook处理与签名验证
app-webhooks - - 带有通知、模态框与重定向的动作按钮
app-custom-actions - - 应用支付处理器(支持支付/确认/捕获/退款)
app-payment-methods - - 通过manifest定义自定义字段
app-custom-fields - - 基于Twig的应用脚本与钩子点
app-scripts
18. Integration Patterns (HIGH)
18. 集成模式(高)
- - Sync/async payment handler patterns
integration-payment-handler - - Shipping calculators and delivery times
integration-shipping-method - - CMS element creation with data resolvers
integration-cms-elements - - Import/export profiles and converters
integration-import-export - - HTTP client patterns with retry and caching
integration-external-api
- - 同步/异步支付处理器模式
integration-payment-handler - - 物流计算器与配送时间
integration-shipping-method - - 带有数据解析器的CMS元素创建
integration-cms-elements - - 导入/导出配置文件与转换器
integration-import-export - - 带有重试与缓存的HTTP客户端模式
integration-external-api
19. CLI Commands (MEDIUM)
19. CLI命令(中)
- - Custom command creation with arguments and options
cli-commands - - Configure/initialize/interact/execute lifecycle
cli-command-lifecycle - - SymfonyStyle, progress bars, and formatting
cli-progress-output
- - 创建带有参数与选项的自定义命令
cli-commands - - 配置/初始化/交互/执行生命周期
cli-command-lifecycle - - 使用SymfonyStyle、进度条与格式化输出
cli-progress-output
20. Multi-Channel & B2B (MEDIUM-HIGH)
20. 多渠道与B2B(中高)
- - Sales channel awareness and visibility
multichannel-saleschannel - - Company handling, roles, and budgets
multichannel-b2b-patterns - - Currency, customer groups, and tax handling
multichannel-pricing - - Context types, scopes, and permissions
multichannel-context
- - 销售渠道感知与可见性
multichannel-saleschannel - - 企业账户处理、角色与预算
multichannel-b2b-patterns - - 货币、客户组与税务处理
multichannel-pricing - - 上下文类型、作用域与权限
multichannel-context
21. DevOps & Tooling (MEDIUM)
21. DevOps与工具(中)
- - Dockware, docker-compose, Makefile patterns
devops-development-setup - - Deployment scripts, builds, and rollbacks
devops-deployment - - PHPStan, PHP-CS-Fixer, Rector configuration
devops-static-analysis - - Profiler, Xdebug, logging, and data collectors
devops-debugging - - GitHub Actions and GitLab CI workflows
devops-ci-cd
- - Dockware、docker-compose、Makefile模式
devops-development-setup - - 部署脚本、构建与回滚
devops-deployment - - PHPStan、PHP-CS-Fixer、Rector配置
devops-static-analysis - - 分析器、Xdebug、日志与数据收集器
devops-debugging - - GitHub Actions与GitLab CI工作流
devops-ci-cd
22. Common Patterns (HIGH)
22. 通用模式(高)
- - Custom exceptions and error pages
pattern-error-handling - - Snippets and entity translations
pattern-translations - - Media service, uploads, and thumbnails
pattern-media-handling - - Custom rule conditions for pricing/shipping
pattern-rule-builder - - Version-aware code and update migrations
pattern-upgrade-migration
- - 自定义异常与错误页面
pattern-error-handling - - 代码片段与实体翻译
pattern-translations - - 媒体服务、上传与缩略图
pattern-media-handling - - 用于定价/物流的自定义规则条件
pattern-rule-builder - - 版本感知代码与更新迁移
pattern-upgrade-migration
Core Principles
核心原则
The Decorator Pattern is Key
装饰器模式是核心
Never modify core files. Always use:
- Decorators for services
- Event subscribers for hooks
- Entity extensions for data
永远不要修改核心文件。请始终使用:
- 装饰器用于服务定制
- 事件订阅器用于钩子
- 实体扩展用于数据扩展
DAL Over Raw SQL
优先使用DAL而非原生SQL
Use Shopware's Data Abstraction Layer instead of raw SQL:
- Type safety and validation
- Automatic association handling
- Built-in versioning and translation
使用Shopware的数据抽象层(DAL)替代原生SQL:
- 类型安全与验证
- 自动关联处理
- 内置版本控制与翻译
Performance First
性能优先
Shopware can be slow if misused:
- Enable HTTP cache in production
- Use Elasticsearch for 10K+ products
- Load only needed associations
- Use message queue for heavy tasks
若使用不当,Shopware可能会运行缓慢:
- 生产环境启用HTTP缓存
- 商品数量超过10K时使用Elasticsearch
- 仅加载所需的关联
- 重型任务使用消息队列
How to Use
使用方法
Read individual rule files for detailed explanations and code examples:
rules/plugin-structure.md
rules/custom-decorator-pattern.md
rules/perf-http-cache.md
rules/security-input-validation.md
rules/dal-criteria-usage.mdEach rule file contains:
- Brief explanation of why it matters
- Incorrect code example with explanation
- Correct code example with explanation
- Additional context and references
阅读单个规则文件获取详细说明与代码示例:
rules/plugin-structure.md
rules/custom-decorator-pattern.md
rules/perf-http-cache.md
rules/security-input-validation.md
rules/dal-criteria-usage.md每个规则文件包含:
- 规则重要性的简要说明
- 错误代码示例及解释
- 正确代码示例及解释
- 额外背景与参考资料
Full Compiled Document
完整编译文档
For the complete guide with all rules expanded:
AGENTS.md包含所有规则详细内容的完整指南:
AGENTS.md