rezasanaeefar/magento-product-review
GitHub: rezasanaeefar/magento-product-review
一个严格遵循 Magento 2 架构规范的商品评论 REST API 模块,提供完整的 CRUD 接口并兼具惯用模块设计的参考价值。
Stars: 0 | Forks: 0
# Sana_ProductReview — Magento 2 REST API 模块
一个简洁且经过充分测试的 Magento 2 模块,它提供了一个 **Product Review**
REST API。它严格遵循 Magento 的架构
规范,因此也可以作为惯用模块设计的参考范例。
## 为什么该模块展示了优秀的 Magento 实践
- **服务契约优先** — 一个公共的 `Api\` 接口层
(`ReviewRepositoryInterface`、`ReviewInterface`),供 Web API 和
内部调用者共享。
- **依赖注入** — 实现通过 `etc/di.xml` 绑定;
不直接使用 `ObjectManager`。
- **声明式 schema** — 表在 `db_schema.xml` 中定义
并配有相应的白名单(这是现代的做法,而不是 `InstallSchema`)。
- **Data patch** — 示例数据通过受跟踪的 `DataPatchInterface` 添加。
- **完善的错误处理** — 失败会被转换为带类型的
Magento 异常(`CouldNotSaveException`、`NoSuchEntityException`),
Web API 会将其映射为正确的 HTTP 状态码。
- **受 ACL 保护的写操作** — 读取端点是匿名的,写入
端点需要 ACL 资源。
- **单元测试** — PHPUnit 测试在隔离环境下覆盖了 repository 的验证
和异常行为。
## 环境要求
- Magento 2.4.x
- PHP 8.1+
## 安装说明
将模块复制到你的 Magento 安装目录中:
```
cp -R Sana/ProductReview app/code/Sana/ProductReview
```
然后启用它:
```
bin/magento module:enable Sana_ProductReview
bin/magento setup:upgrade
bin/magento setup:di:compile
bin/magento cache:flush
```
## REST API 端点
| 方法 | 端点 | 鉴权 | 描述 |
|--------|-------------------------|-------------|--------------------------|
| GET | `/V1/reviews` | 匿名 | 列出评论(支持分页) |
| GET | `/V1/reviews/:reviewId` | 匿名 | 获取单条评论 |
| POST | `/V1/reviews` | ACL token | 创建评论 |
| DELETE | `/V1/reviews/:reviewId` | ACL token | 删除评论 |
### 示例:创建评论
```
curl -X POST "https:///rest/V1/reviews" \
-H "Authorization: Bearer " \
-H "Content-Type: application/json" \
-d '{
"review": {
"product_id": 1,
"customer_name": "Jane Doe",
"rating": 5,
"title": "Great product",
"content": "Exactly as described."
}
}'
```
### 示例:列出评论
```
curl "https:///rest/V1/reviews?searchCriteria[pageSize]=20"
```
## 运行测试
在模块目录下运行:
```
composer install
vendor/bin/phpunit --testsuite "Sana_ProductReview Unit Tests"
```
或者通过 Magento 的测试运行器在 Magento 根目录下运行:
```
vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist \
app/code/Sana/ProductReview/Test/Unit
```
## 模块结构
```
Sana/ProductReview/
├── Api/
│ ├── Data/
│ │ ├── ReviewInterface.php
│ │ └── ReviewSearchResultsInterface.php
│ └── ReviewRepositoryInterface.php # service contract
├── Model/
│ ├── Review.php # entity model
│ ├── ReviewRepository.php # business logic
│ └── ResourceModel/
│ ├── Review.php
│ └── Review/Collection.php
├── Setup/Patch/Data/AddSampleReview.php
├── Test/Unit/Model/ReviewRepositoryTest.php
├── etc/
│ ├── acl.xml
│ ├── di.xml
│ ├── db_schema.xml
│ ├── db_schema_whitelist.json
│ ├── module.xml
│ └── webapi.xml # REST route mapping
├── composer.json
├── registration.php
└── README.md
```
## 许可证
MIT — 详见 [LICENSE](LICENSE)。
# magento-product-review
标签:ffuf, Magento 2, OpenVAS, PHP, RESTful API, Syscall, Web开发, 商品评价, 提示词优化, 电商系统