Installation:
composer require altblock/comment-bundle
Run bundle installation command:
php bin/console fos:comment:install
This generates the required database tables (if using Doctrine) and config files.
Enable the Bundle:
Add to config/bundles.php:
return [
// ...
AltBlock\CommentBundle\AltBlockCommentBundle::class => ['all' => true],
];
First Use Case: Display comments on a page:
{% render 'AltBlockCommentBundle:Comment:thread.html', {
'subject': commentableEntity,
'template': 'AltBlockCommentBundle:Comment:thread.html.twig'
} %}
Ensure your entity is annotated with @Commentable:
use AltBlock\CommentBundle\Model\CommentableInterface;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="blog_posts")
*/
class BlogPost implements CommentableInterface
{
// ...
}
Comment Submission:
Use the fos_comment_form type in Symfony forms:
$form = $this->createFormBuilder($comment)
->add('content', 'fos_comment_text', [
'attr' => ['class' => 'form-control']
])
->getForm();
Submit via a controller:
$comment = new Comment();
$comment->setSubject($commentableEntity);
$form->handleRequest($request);
if ($form->isValid()) {
$em->persist($comment);
$em->flush();
}
Thread Rendering: Customize the thread template by extending:
{% extends 'AltBlockCommentBundle:Comment:thread.html.twig' %}
{% block comment_content %}
{{- comment.content -}}
{% if comment.author %}
<small>By {{ comment.author.username }}</small>
{% endif %}
{% endblock %}
API Integration (FOSRestBundle): Enable REST API by configuring routing:
# config/routes.yaml
fos_comment_rest:
resource: "@AltBlockCommentBundle/Resources/config/routing/rest.xml"
prefix: /api/comments
Access endpoints like /api/comments/{subject}.
Event-Driven Extensions:
Subscribe to lifecycle events (e.g., fos_comment_pre_persist):
// src/EventListener/CommentListener.php
public function onPrePersist(PrePersistCommentEvent $event)
{
$comment = $event->getComment();
$comment->setIpAddress($event->getRequest()->getClientIp());
}
Register in services.yaml:
services:
App\EventListener\CommentListener:
tags:
- { name: kernel.event_listener, event: fos_comment_pre_persist, method: onPrePersist }
FOSUserBundle:
Link comments to users by implementing CommentableInterface in your user entity:
class User implements CommentableInterface
{
// ...
}
Configure in config.yml:
fos_comment:
db_driver: orm
class:
model:
comment: App\Entity\Comment
thread: App\Entity\Thread
form: App\Form\CommentType
service:
comment_manager: fos_comment.manager.default
acl: true # Enable ACL if using FOSUserBundle
Akismet Integration:
Enable in config.yml:
fos_comment:
akismet:
enabled: true
key: "your_akismet_key"
blog: "your_blog_url"
Markup Parsing:
Configure HtmlPurifier or php-sundown in config.yml:
fos_comment:
markup_parser: htmlpurifier
htmlpurifier:
config: "@altblock_comment.htmlpurifier.config"
Custom Sorting:
Override the default sort order (e.g., by createdAt):
fos_comment:
thread_sort:
method: "createdAt"
order: DESC
Entity Mapping:
Comment and Thread entities extend the base classes:
use AltBlock\CommentBundle\Model\Comment as BaseComment;
class Comment extends BaseComment { ... }
@Commentable on entities will break thread rendering.Database Schema:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
Caching Threads:
php bin/console cache:clear
config.yml if needed:
fos_comment:
thread_cache: false
REST API Quirks:
FOSRestBundle is installed and configured for API routes.jms/serializer-bundle:
composer require jms/serializer-bundle
Event Debugging:
public function onPrePersist(PrePersistCommentEvent $event)
{
dump($event->getComment(), $event->getRequest());
}
Template Overrides:
debug:config to verify template paths:
php bin/console debug:config altblock_comment
Acl Issues:
fos_comment.acl.provider is configured in security.yml.Performance:
$comments = $commentManager->findCommentsBySubject($subject, 0, 10);
Custom Form Fields: Extend the form type:
class CustomCommentType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('rating', 'integer', [
'label' => 'Rating',
'attr' => ['class' => 'rating-input']
]);
}
}
Update config.yml:
fos_comment:
class:
form: App\Form\CustomCommentType
Custom Validation:
Add constraints to the Comment entity:
use Symfony\Component\Validator\Constraints as Assert;
class Comment extends BaseComment
{
/**
* @Assert\Length(min=10)
*/
protected $content;
}
Custom Thread Storage:
Implement ThreadManagerInterface for non-Doctrine backends (e.g., MongoDB):
class MongoThreadManager implements ThreadManagerInterface
{
// Implement required methods
}
Register as a service:
services:
fos_comment.manager.thread:
class: App\Service\MongoThreadManager
tags:
- { name: fos_comment.thread_manager }
How can I help you explore Laravel packages today?