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

awaresoft/comment-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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
    
  2. Enable in AppKernel.php

    new Awaresoft\CommentBundle\AwaresoftCommentBundle(),
    
  3. 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
    
  4. First Use Case: Basic Commenting

    • Admin Setup: Register the 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" }
      
    • Frontend Display: Use Sonata’s block system to render comments on entities:
      {{ sonata_block_service('sonata.block.service.comment', { 'template': 'AwaresoftCommentBundle:Comment:block.html.twig' }) }}
      

Implementation Patterns

Core Workflows

  1. Commenting on Entities

    • Attach Comments to Models: Use the Commentable trait or interface to mark entities:
      use Awaresoft\CommentBundle\Model\CommentableInterface;
      
      class Article implements CommentableInterface {
          // ...
      }
      
    • Create Comments via API:
      $comment = new \Awaresoft\CommentBundle\Entity\Comment();
      $comment->setContent('Great post!');
      $comment->setAuthor($this->getUser());
      $comment->setCommentable($article);
      $em->persist($comment);
      $em->flush();
      
  2. Sonata Integration

    • Admin Customization: Extend 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']);
          }
      }
      
    • Blocks: Use pre-built blocks (e.g., comment_list, comment_form) in Twig:
      {% block sonata_block %}
          {{ sonata_block_service('sonata.block.service.comment_list', { 'template': 'custom_template' }) }}
      {% endblock %}
      
  3. Validation and Moderation

    • Enable Moderation: Configure in config.yml:
      awaresoft_comment:
          moderation: true
      
    • Custom Validation: Override the Comment entity validator:
      $comment->setContent($request->get('content'));
      $validator = $this->get('validator');
      $errors = $validator->validate($comment);
      
  4. Notifications

    • Event Listeners: Subscribe to CommentEvents (e.g., CommentCreatedEvent) to trigger emails or actions:
      $eventDispatcher->addListener(
          'comment.created',
          function (CommentCreatedEvent $event) {
              // Send notification to author
          }
      );
      

Gotchas and Tips

Common Pitfalls

  1. Symlinking Issues

    • Problem: Forgetting to remove Composer’s autoload entry for the vendor after symlinking.
    • Fix: Manually edit vendor/composer/autoload_psr4.php to remove the old path, then clear cache:
      php bin/console cache:clear
      
  2. Sonata Version Mismatch

    • Problem: The bundle requires Sonata 3.x. Using Sonata 4.x+ may break functionality.
    • Fix: Pin Sonata dependencies in composer.json:
      "sonata-project/admin-bundle": "3.*",
      "sonata-project/block-bundle": "3.*"
      
  3. Migration Conflicts

    • Problem: Schema updates in newer versions may conflict with existing migrations.
    • Fix: Compare the bundle’s Resources/config/doctrine schema with your database and merge changes manually.
  4. Permission Denied Errors

    • Problem: Comments not saving due to ACL or ownership issues.
    • Fix: Ensure the Comment entity’s author field is set to the current user:
      $comment->setAuthor($this->getUser());
      

Debugging Tips

  • Enable SQL Logging: Add to config.yml to debug queries:
    doctrine:
        dbal:
            logging: true
    
  • Check Events: Verify listeners are subscribed by dumping the dispatcher:
    dump($this->get('event_dispatcher')->getListeners());
    
  • Override Templates: Copy templates from AwaresoftCommentBundle:Comment to your theme’s templates/AwaresoftCommentBundle/Comment to customize without modifying the bundle.

Extension Points

  1. Custom Comment Types

    • Extend the 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;
          }
      }
      
  2. API Endpoints

    • Use FOSRestBundle to expose comment actions:
      # config/routing.yml
      awaresoft_comment_rest:
          resource: "@AwaresoftCommentBundle/Resources/config/routing/rest.yml"
          prefix: /api
      
  3. Webhooks

    • Trigger external services on comment events via CommentEvents:
      $eventDispatcher->addListener(
          'comment.created',
          function (CommentCreatedEvent $event) {
              $client = new \GuzzleHttp\Client();
              $client->post('https://webhook.example.com', [
                  'json' => ['comment_id' => $event->getComment()->getId()]
              ]);
          }
      );
      
  4. Testing

    • Use functional tests with the CommentAdmin:
      $client = static::createClient();
      $crawler = $client->request('GET', '/admin/comments');
      $this->assertEquals(1, $crawler->filter('table tr')->count());
      
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