Installation
Add the bundle via Composer (if using Satis) or symlink to /src/Awaresoft:
composer require awaresoft/comment-bundle
Or manually clone and symlink:
mkdir -p src/Awaresoft
git clone [repo-url] src/Awaresoft/CommentBundle
Enable in AppKernel.php
new Awaresoft\CommentBundle\AwaresoftCommentBundle(),
Configure Database
Run migrations (if provided) or manually create tables based on the bundle’s Resources/config/doctrine schema:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
First Use Case: Basic Commenting
CommentAdmin in your services.yml:
services:
sonata.admin.comment:
class: Awaresoft\CommentBundle\Admin\CommentAdmin
tags:
- { name: sonata.admin, manager_type: orm, group: "Content", label: "Comments" }
{{ sonata_block_service('sonata.block.service.comment', { 'template': 'AwaresoftCommentBundle:Comment:block.html.twig' }) }}
Commenting on Entities
Commentable trait or interface to mark entities:
use Awaresoft\CommentBundle\Model\CommentableInterface;
class Article implements CommentableInterface {
// ...
}
$comment = new \Awaresoft\CommentBundle\Entity\Comment();
$comment->setContent('Great post!');
$comment->setAuthor($this->getUser());
$comment->setCommentable($article);
$em->persist($comment);
$em->flush();
Sonata Integration
CommentAdmin to add fields or filters:
class CustomCommentAdmin extends CommentAdmin {
protected function configureListFields(ListMapper $listMapper) {
$listMapper->add('content');
$listMapper->add('author.username');
$listMapper->add('createdAt', null, ['template' => 'AwaresoftCommentBundle:Comment:list_date.html.twig']);
}
}
comment_list, comment_form) in Twig:
{% block sonata_block %}
{{ sonata_block_service('sonata.block.service.comment_list', { 'template': 'custom_template' }) }}
{% endblock %}
Validation and Moderation
config.yml:
awaresoft_comment:
moderation: true
Comment entity validator:
$comment->setContent($request->get('content'));
$validator = $this->get('validator');
$errors = $validator->validate($comment);
Notifications
CommentEvents (e.g., CommentCreatedEvent) to trigger emails or actions:
$eventDispatcher->addListener(
'comment.created',
function (CommentCreatedEvent $event) {
// Send notification to author
}
);
Symlinking Issues
vendor/composer/autoload_psr4.php to remove the old path, then clear cache:
php bin/console cache:clear
Sonata Version Mismatch
composer.json:
"sonata-project/admin-bundle": "3.*",
"sonata-project/block-bundle": "3.*"
Migration Conflicts
Resources/config/doctrine schema with your database and merge changes manually.Permission Denied Errors
Comment entity’s author field is set to the current user:
$comment->setAuthor($this->getUser());
config.yml to debug queries:
doctrine:
dbal:
logging: true
dump($this->get('event_dispatcher')->getListeners());
AwaresoftCommentBundle:Comment to your theme’s templates/AwaresoftCommentBundle/Comment to customize without modifying the bundle.Custom Comment Types
Comment entity or create a new entity (e.g., Reply) that references Comment:
class Reply extends Comment {
private $comment;
public function setComment(Comment $comment) {
$this->comment = $comment;
}
}
API Endpoints
# config/routing.yml
awaresoft_comment_rest:
resource: "@AwaresoftCommentBundle/Resources/config/routing/rest.yml"
prefix: /api
Webhooks
CommentEvents:
$eventDispatcher->addListener(
'comment.created',
function (CommentCreatedEvent $event) {
$client = new \GuzzleHttp\Client();
$client->post('https://webhook.example.com', [
'json' => ['comment_id' => $event->getComment()->getId()]
]);
}
);
Testing
CommentAdmin:
$client = static::createClient();
$crawler = $client->request('GET', '/admin/comments');
$this->assertEquals(1, $crawler->filter('table tr')->count());
How can I help you explore Laravel packages today?