Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Comment Bundle Laravel Package

boostworld/comment-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Install via Composer:

    composer require boostworld/comment-bundle
    

    (Note: The forked boostworld/comment-bundle is PHP8-compatible; verify compatibility with your Symfony version.)

  2. Enable the Bundle: Add to config/bundles.php:

    Boostworld\CommentBundle\BoostworldCommentBundle::class => ['all' => true],
    
  3. Configure Database: Run migrations (Doctrine ORM example):

    php bin/console doctrine:migrations:diff
    php bin/console doctrine:migrations:migrate
    
  4. First Use Case: Display comments on a blog post:

    {% render 'FOSCommentBundle:Comment:thread.html' %}
    

    (Ensure fos_comment is configured in config/packages/fos_comment.yaml.)


Key First Steps

  • Check config/packages/fos_comment.yaml: Review default settings (e.g., driver, class, db_driver).
  • Verify Routes: The bundle adds /comment/{id} and /comment/thread/{object} routes.
  • Test the API: If using FOSRestBundle, hit /api/comments to validate REST endpoints.

Implementation Patterns

Core Workflows

  1. Embedding Comments in Twig:

    {% render 'FOSCommentBundle:Comment:thread.html', {
        object: post,
        template: 'FOSCommentBundle:Comment:thread.html.twig'
    } %}
    
    • Use object to tie comments to a model (e.g., App\Entity\Post).
    • Customize templates by overriding templates/comment/thread.html.twig.
  2. Form Integration:

    // In a controller
    $form = $this->createForm(CommentType::class, $comment);
    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        $em->persist($comment);
        $em->flush();
    }
    
    • Extend CommentType for custom fields (e.g., rating).
  3. Event-Driven Extensions: Listen to fos_comment_post_create or fos_comment_post_update:

    // src/EventListener/CommentListener.php
    public function onPostCreate(PostCommentEvent $event) {
        $comment = $event->getComment();
        // Add logic (e.g., notifications, analytics)
    }
    

    Register in services.yaml:

    services:
        App\EventListener\CommentListener:
            tags:
                - { name: kernel.event_listener, event: fos_comment_post_create, method: onPostCreate }
    
  4. API Usage (FOSRestBundle):

    • Create: POST /api/comments with JSON payload:
      { "object": "/posts/1", "author": "user@example.com", "body": "Great post!" }
      
    • List: GET /api/comments?object=/posts/1.

Integration Tips

  • Doctrine Relations: Ensure your Post entity has a OneToMany relation to Comment:
    #[ORM\OneToMany(targetEntity: Comment::class, mappedBy: 'object')]
    private Collection $comments;
    
  • Security: Use Symfony’s voter system to restrict comment editing/deletion:
    // src/Security/Voter/CommentVoter.php
    public function supports($attribute, $subject) {
        return $attribute === 'EDIT' && $subject instanceof Comment;
    }
    
  • Markup Parsing: Enable html_purifier in config:
    fos_comment:
        db_driver: orm
        class:
            model:
                comment: App\Entity\Comment
        form:
            type: fos_comment_form
            options:
                markup_parser: html_purifier
    

Gotchas and Tips

Pitfalls

  1. PHP8 Compatibility:

    • The boostworld fork may have untested edge cases. Test with:
      php bin/phpunit
      
    • Watch for TypeError in event listeners (e.g., PostCommentEvent arguments).
  2. Doctrine Proxy Issues:

    • If comments fail to load, clear the cache:
      php bin/console cache:clear
      
    • Ensure Comment entity uses #[ORM\HasLifecycleCallbacks] if using soft deletes.
  3. Thread Sorting:

    • Default sorting is by createdAt DESC. Override in config:
      fos_comment:
          thread_sorting: { field: 'createdAt', direction: 'ASC' }
      
  4. REST API Quirks:

    • FOSRestBundle must be installed (composer require nelmio/api-doc-bundle for docs).
    • Validate CSRF tokens for POST/PUT requests if using Symfony’s security.

Debugging Tips

  1. Enable Debugging: Add to config/packages/dev/fos_comment.yaml:

    fos_comment:
        debug: true
    

    Logs events to var/log/dev.log.

  2. Common Errors:

    • "No route found for 'fos_comment_thread'": Ensure the route is loaded (php bin/console debug:router | grep fos_comment).
    • "Invalid object reference": Verify the object field in Comment matches your entity’s route (e.g., /posts/1).
  3. Event Debugging: Dump events in a listener:

    public function onPostCreate(PostCommentEvent $event) {
        dump($event->getComment()->getObject()); // Check object binding
    }
    

Extension Points

  1. Custom Fields: Extend CommentType:

    // src/Form/Type/CustomCommentType.php
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder->add('rating', IntegerType::class, ['attr' => ['min' => 1, 'max' => 5]]);
    }
    

    Update config:

    fos_comment:
        form:
            type: App\Form\Type\CustomCommentType
    
  2. Custom Templates: Override:

    • templates/comment/thread.html.twig (main thread)
    • templates/comment/form.html.twig (form styling)
    • templates/comment/comment.html.twig (individual comment).
  3. Database Drivers: Switch to MongoDB ODM by updating db_driver and class.model.comment in config.

  4. Akismet Integration: Enable in config:

    fos_comment:
        akismet:
            enabled: true
            key: 'your_api_key'
            blog: 'http://your-site.com'
    

    Requires composer require fos/http-cache.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
comsave/common
alecsammon/php-raml-parser
chrome-php/wrench
lendable/composer-license-checker
typhoon/reflection
mesilov/moneyphp-percentage
mike42/gfx-php
bookdown/themes
aura/view
aura/html
aura/cli
povils/phpmnd
nayjest/manipulator
omnipay/tests
psr-mock/http-message-implementation
psr-mock/http-factory-implementation
psr-mock/http-client-implementation
voku/email-check
voku/urlify
rtheunissen/guzzle-log-middleware