tourze/product-comment-bundle
最新稳定版本:0.0.1
Composer 安装命令:
composer require tourze/product-comment-bundle
包简介
Symfony 商品评论系统,支持评论、回复、点赞功能
README 文档
README
A Symfony bundle for managing product comments, including hierarchical comments, ratings, likes, and administrative controls.
Table of Contents
- Quick Start
- Features
- Installation
- Dependencies
- API Endpoints
- Entity Structure
- Advanced Usage
- Security
- Contributing
- License
Quick Start
Register the Bundle
Add the bundle to your config/bundles.php:
<?php return [ // ... ProductCommentBundle\ProductCommentBundle::class => ['all' => true], ];
Configure Database
Run the database migrations to create the necessary tables:
php bin/console doctrine:migrations:migrate
Basic Usage
Submit a Product Comment
<?php use ProductCommentBundle\Procedure\SubmitProductComment; // Through JSON-RPC API $procedure = new SubmitProductComment($orderProductRepository, $productCommentRepository, $requestStack, $security, $entityManager); $procedure->orderProductId = 'order-product-123'; $procedure->content = 'Great product! Highly recommended.'; $procedure->images = ['image1.jpg', 'image2.jpg']; $result = $procedure->execute();
Get Product Comments
<?php use ProductCommentBundle\Procedure\GetProductCommentList; $procedure = new GetProductCommentList($security, $productCommentRepository, $spuService); $procedure->productId = 'product-123'; $procedure->skuId = 'sku-456'; // optional $procedure->rootParentId = '0'; // optional, defaults to '0' for top-level comments $comments = $procedure->execute();
Like a Comment
<?php use ProductCommentBundle\Procedure\LikeProductComment; $procedure = new LikeProductComment($productCommentRepository, $entityManager, $security); $procedure->contentId = 'comment-123'; $result = $procedure->execute();
Configuration
The bundle uses the following configuration structure:
# config/packages/product_comment.yaml product_comment: # Configuration will be added here as needed
Features
- 🔗 Hierarchical comment system with parent-child relationships
- ⭐ Product rating system with numeric scores
- 👍 Like/dislike functionality with tracking
- 🖼️ Support for image and video attachments
- 🔐 User authentication and authorization
- 📱 IP tracking and client identification
- 👨💼 Administrative review and management
- 🎯 JSON-RPC API endpoints for frontend integration
- 🔧 EasyAdmin integration for backend management
Installation
composer require tourze/product-comment-bundle
Dependencies
Required Dependencies
- PHP 8.1 or higher
- Symfony 6.4 or higher
- Doctrine ORM 3.0 or higher
- EasyAdmin Bundle 4.0 or higher
Internal Dependencies
tourze/bundle-dependency- Bundle dependency managementtourze/doctrine-indexed-bundle- Doctrine indexing utilitiestourze/doctrine-snowflake-bundle- Snowflake ID generationtourze/json-rpc-core- JSON-RPC core functionalitytourze/json-rpc-paginator-bundle- Pagination support
API Endpoints
The bundle provides the following JSON-RPC procedures:
SubmitProductComment- Submit a new product commentGetProductCommentList- Retrieve paginated comment listsLikeProductComment- Like or unlike a comment (requirescontentIdparameter)ReplyProductComment- Reply to an existing comment (requirescontentIdandcontentparameters)
Entity Structure
ProductComment
- Hierarchical structure with parent/child relationships
- Support for ratings, likes, and multimedia content
- User authentication and IP tracking
- Administrative controls and state management
ProductCommentLike
- Tracks user likes/dislikes on comments
- Prevents duplicate likes from same user
CommentLikeLog
- Audit trail for like/unlike actions
- Tracks user actions and timestamps
Advanced Usage
Custom Comment Validation
You can extend the comment validation by implementing custom validators:
use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Context\ExecutionContextInterface; class CustomCommentValidator { public function validate($value, ExecutionContextInterface $context) { // Your custom validation logic here if (strlen($value) < 10) { $context->buildViolation('Comment must be at least 10 characters long') ->addViolation(); } } }
Event Subscribers
The bundle dispatches events that you can listen to:
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use ProductCommentBundle\Event\CommentCreatedEvent; class CommentSubscriber implements EventSubscriberInterface { public static function getSubscribedEvents() { return [ CommentCreatedEvent::class => 'onCommentCreated', ]; } public function onCommentCreated(CommentCreatedEvent $event) { // Handle comment creation } }
Custom Admin Controllers
Extend the provided admin controllers for custom functionality:
use ProductCommentBundle\Controller\Admin\ProductCommentCrudController; class CustomProductCommentCrudController extends ProductCommentCrudController { public function configureActions(Actions $actions): Actions { // Your custom actions return parent::configureActions($actions); } }
Security
Authentication
The bundle requires authenticated users for comment operations. Ensure your application has proper authentication configured:
# config/packages/security.yaml security: access_control: - { path: ^/admin/product-comment, roles: ROLE_ADMIN } - { path: ^/api/product-comment, roles: ROLE_USER }
Authorization
User permissions are checked at the procedure level using #[IsGranted] attributes:
- Comment submission requires
IS_AUTHENTICATED_FULLY - Admin operations require
ROLE_ADMIN - Like operations require
IS_AUTHENTICATED_FULLY
Data Validation
All input data is validated using Symfony's validation component:
- Content length validation
- Image format validation
- User ownership verification
- Rate limiting protection
IP Tracking
The bundle tracks IP addresses for security and auditing purposes. This data is used for:
- Preventing abuse
- Audit trails
- Geographic analytics
Contributing
Please see CONTRIBUTING.md for details on how to contribute to this project.
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-14