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

Ccdn Component Attachment Bundle Laravel Package

codeconsortium/ccdn-component-attachment-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require codeconsortium/ccdn-component-attachment-bundle:dev-master
    

    (Note: Bundle is outdated; ensure Symfony 2.1.x/PHP 5.4 compatibility in your project.)

  2. Enable the Bundle Add to app/AppKernel.php:

    new CodeConsortium\CCDNComponentAttachmentBundle\CCDNComponentAttachmentBundle(),
    
  3. Configure Database (Optional) The bundle uses Doctrine 2.1.x but does not require a specific DB schema. Ensure your config.yml includes:

    doctrine:
        dbal:
            driver:   "%database_driver%"
            host:     "%database_host%"
            dbname:   "%database_name%"
            user:     "%database_user%"
            password: "%database_password%"
            charset:  UTF8
    
  4. First Use Case: Uploading Attachments

    • Extend the Attachment entity or use the provided controller (AttachmentController).
    • Example route in routing.yml:
      ccdn_attachment:
          resource: "@CCDNComponentAttachmentBundle/Resources/config/routing.yml"
          prefix:   /attachments
      
    • Access upload form at /attachments/new.

Implementation Patterns

Core Workflows

  1. Uploading Files

    • Use the AttachmentType form (Symfony 2.1 form builder) to handle uploads:
      $form = $this->createForm(new AttachmentType());
      
    • Files are stored in uploads/ (default) or a configurable directory. Thumbnails are auto-generated for images.
  2. Attaching to Entities

    • Link attachments to models (e.g., Post, Comment) via a many-to-many relationship:
      /**
       * @ORM\ManyToMany(targetEntity="CodeConsortium\CCDNComponentAttachmentBundle\Entity\Attachment")
       */
      private $attachments;
      
    • Use the AttachmentListener to auto-populate attachments during entity persistence.
  3. Displaying Attachments

    • Render thumbnails in templates:
      {% for attachment in post.attachments %}
          <img src="{{ asset(attachment.getThumbnailPath()) }}" alt="{{ attachment.name }}">
          <a href="{{ path('ccdn_attachment_download', {'id': attachment.id}) }}">Download</a>
      {% endfor %}
      
    • Use the AttachmentHelper for utility methods (e.g., getFileExtension()).
  4. Deleting Files

    • Call the delete() method on the Attachment entity or use the controller’s deleteAction:
      $attachment->delete(); // Handles file + DB cleanup
      

Integration Tips

  • Custom Storage: Override the AttachmentStorage service to use S3/AWS:
    # app/config/config.yml
    ccdn_attachment.storage:
        class: Your\Custom\Storage\Service
        arguments: ["@your_s3_client"]
    
  • Validation: Extend AttachmentType to add constraints:
    $builder->add('file', 'file', [
        'max_size' => '10M',
        'mime_types' => ['image/jpeg', 'application/pdf'],
    ]);
    
  • Events: Listen for attachment.pre_upload or attachment.post_delete to hook into workflows:
    $dispatcher->addListener('attachment.pre_upload', function($event) {
        // Log or transform files before upload
    });
    

Gotchas and Tips

Pitfalls

  1. Symfony 2.1 Legacy Code

    • The bundle uses deprecated Symfony 2.1 components (e.g., FormType, Templating). Update or wrap calls if using newer Symfony.
    • Example fix for FormType:
      // Replace:
      $form = $this->createForm(new AttachmentType());
      // With:
      $form = $this->createForm(AttachmentType::class);
      
  2. Thumbnail Generation

    • Thumbnails fail silently if Imagick/GD is missing. Add a fallback:
      if (!$attachment->hasThumbnail()) {
          $attachment->setThumbnailPath('/path/to/fallback.png');
      }
      
    • Configure allowed image types in config.yml:
      ccdn_attachment:
          allowed_thumbnail_types: [jpeg, png, gif]
      
  3. File Permissions

    • Ensure the uploads/ directory is writable:
      chmod -R 775 app/uploads
      
    • For shared hosting, use umask in your upload script.
  4. Database Schema

    • The bundle assumes a default Attachment entity. If extending, regenerate migrations:
      php app/console doctrine:schema:update --force
      

Debugging Tips

  • Log Uploads: Enable Doctrine logging to track attachment creation:
    doctrine:
        dbal:
            logging: true
    
  • Check File Paths: Verify paths in AttachmentStorage:
    $this->get('ccdn_attachment.storage')->getUploadPath(); // Should return absolute path
    
  • Clear Cache: After config changes:
    php app/console cache:clear
    

Extension Points

  1. Custom File Processors

    • Implement CodeConsortium\CCDNComponentAttachmentBundle\Processor\FileProcessorInterface to add watermarks, virus scanning, etc.:
      class WatermarkProcessor implements FileProcessorInterface {
          public function process(File $file) {
              // Add watermark logic
              return $file;
          }
      }
      
    • Register in services.yml:
      ccdn_attachment.processor.watermark:
          class: Your\WatermarkProcessor
          tags:
              - { name: ccdn_attachment.processor }
      
  2. Override Templates

    • Copy Resources/views/ from the bundle to app/Resources/CCDNComponentAttachmentBundle/ to customize Twig templates.
  3. API Endpoints

    • Extend AttachmentController to add JSON responses:
      public function apiUploadAction(Request $request) {
          $form = $this->createForm(AttachmentType::class);
          $form->handleRequest($request);
          if ($form->isSubmitted()) {
              return new JsonResponse(['success' => true, 'id' => $attachment->getId()]);
          }
          return new JsonResponse(['success' => false], 400);
      }
      
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.
nasirkhan/laravel-sharekit
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity
testo/bridge-symfony