Installation
Add the bundle to your composer.json:
composer require carbocation/comment-bundle
Register the bundle in config/bundles.php:
return [
// ...
Carbocation\CommentBundle\CarbocationCommentBundle::class => ['all' => true],
];
Database Migration Run migrations to create the required tables:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
Basic Configuration
Update config/packages/carbocation_comment.yaml (if it doesn’t exist, create it):
carbocation_comment:
driver: doctrine_orm # Default, but confirm
model:
comment: App\Entity\Comment # Your custom Comment entity
user: App\Entity\User # Your User entity (must implement UserInterface)
First Use Case: Displaying Comments
Use the CommentManager service to fetch and render comments:
use Carbocation\CommentBundle\Manager\CommentManager;
class CommentController extends AbstractController
{
public function show(CommentManager $commentManager, $commentableId, string $commentableType)
{
$comments = $commentManager->getComments($commentableId, $commentableType);
return $this->render('comment/show.html.twig', ['comments' => $comments]);
}
}
Commenting on Entities
Use the CommentManager to attach comments to any entity (e.g., blog posts, products):
$commentManager->addComment(
$commentableEntity, // Your entity (e.g., Post)
$user, // Authorized User
'This is a great article!', // Content
['rating' => 5] // Optional metadata
);
Threaded Replies Reply to a comment by specifying its parent:
$parentComment = $commentManager->getComment($commentId);
$reply = $commentManager->addComment(
$parentComment->getCommentable(),
$user,
'Thanks for your feedback!',
['parent' => $parentComment]
);
Rendering Comments Use Twig extensions for nested rendering:
{% for comment in comments %}
{{ render_comment(comment) }}
{% if comment.children|length > 0 %}
<div class="replies">
{% for child in comment.children %}
{{ render_comment(child) }}
{% endfor %}
</div>
{% endif %}
{% endfor %}
API Integration Expose endpoints via Symfony’s serializer:
# config/packages/serializer.yaml
Carbocation\CommentBundle\Entity\Comment:
attributes:
id: ~
content: ~
createdAt: ~
author: '@api_user_serializer'
CommentEvents (e.g., CommentCreatedEvent) for notifications or analytics.Comment entity to add custom validation (e.g., length, spam checks).CommentEditVoter).Entity Mapping
Comment entity extends Carbocation\CommentBundle\Entity\Comment or implements CommentInterface.User entity, confirm it implements UserInterface with getId() and getUsername().Doctrine ORM Quirks
commentable is a polymorphic relationship. If your entity uses a different strategy (e.g., ManyToOne), override the Commentable trait.Unknown Entity → Verify your commentableType matches the entity’s class name (e.g., App\Entity\Post).Threading Logic
parent_id in the database. If this field is missing, threaded replies will break.php bin/console doctrine:schema:validate to check table structure.Caching
CommentManager::getComments().CARBOCATION_COMMENT_DEBUG=true in .env to log SQL queries and events.CommentEvents:
$dispatcher->addListener(CommentEvents::COMMENT_CREATED, function ($event) {
error_log('Comment created: ' . $event->getComment()->getContent());
});
Custom Fields
Add fields to the Comment entity and update the form type:
// src/Form/CommentType.php
$builder->add('tags', EntityType::class, ['class' => Tag::class]);
Moderation
Implement a CommentState system (e.g., published, pending, spam) via entity lifecycle callbacks.
API Filters
Use Symfony’s ApiPlatform or ApiFilter to paginate/sort comments:
# config/packages/api_platform.yaml
Carbocation\CommentBundle\Entity\Comment:
collectionOperations:
get:
filters: ['comment.filter']
How can I help you explore Laravel packages today?