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

Ical Laravel Package

eluceo/ical

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require eluceo/ical
    
    • No additional configuration required; the package is autoloaded.
  2. First Use Case: Creating a Basic Event

    use Eluceo\iCal\Component\Event;
    use Eluceo\iCal\Component\Calendar;
    use Eluceo\iCal\Property\Transparency;
    
    $event = new Event();
    $event->setUuid('unique-id-here');
    $event->setName('Team Meeting');
    $event->setDescription('Weekly sync with the team');
    $event->setDateStart(\DateTime::createFromFormat('Y-m-d H:i:s', '2024-05-20 10:00:00'));
    $event->setDateEnd(\DateTime::createFromFormat('Y-m-d H:i:s', '2024-05-20 11:00:00'));
    $event->setTransparency(Transparency::TRANSPARENT);
    
    $calendar = new Calendar();
    $calendar->addComponent($event);
    
    // Output as iCal string
    echo $calendar->render();
    
  3. Where to Look First

    • Documentation: Official Docs (if available) or explore the src/ directory for classes like Calendar, Event, Todo, Journal, and their properties.
    • Examples: Check the tests/ folder for practical usage examples (e.g., EventTest.php).
    • Property Classes: Focus on Eluceo\iCal\Property for customizing event attributes (e.g., Attendee, Location, Categories).

Implementation Patterns

Common Workflows

1. Dynamic Event Creation

  • Use Case: Generating events from a database or API.
$events = UserEvent::query()->get();
$calendar = new Calendar();

foreach ($events as $event) {
    $icalEvent = new Event();
    $icalEvent->setUuid($event->uuid);
    $icalEvent->setName($event->title);
    $icalEvent->setDateStart(\DateTime::createFromFormat('Y-m-d H:i:s', $event->start_time));
    $icalEvent->setDateEnd(\DateTime::createFromFormat('Y-m-d H:i:s', $event->end_time));

    // Add custom properties
    $icalEvent->setLocation(new Property\Location($event->location));
    $icalEvent->setCategories([new Property\Categories('work', false)]);

    $calendar->addComponent($icalEvent);
}

// Save to file or return as string
file_put_contents('events.ics', $calendar->render());

2. Recurring Events

  • Use Case: Monthly/weekly meetings.
use Eluceo\iCal\Property\RRule;

$event = new Event();
$event->setName('Monthly Standup');
$event->setDateStart(\DateTime::createFromFormat('Y-m-d H:i:s', '2024-05-20 09:00:00'));
$event->setDateEnd(\DateTime::createFromFormat('Y-m-d H:i:s', '2024-05-20 09:30:00'));
$event->setRRule(new RRule('FREQ=MONTHLY;BYDAY=1MO'));

$calendar->addComponent($event);

3. Parsing iCal Files

  • Use Case: Importing existing .ics files.
use Eluceo\iCal\Component\Parser;

$parser = new Parser();
$components = $parser->parse(file_get_contents('existing.ics'));

foreach ($components as $component) {
    if ($component instanceof Event) {
        // Process imported event
        dd($component->getName(), $component->getDateStart());
    }
}

4. Attendees and RSVP

  • Use Case: Team meetings with attendees.
$event = new Event();
$event->setName('Project Kickoff');
$event->addAttendee(new Property\Attendee('mailto:team@example.com', Property\Attendee::REQUIRED));

// Set RSVP handling
$event->setSequence(1); // Increment on updates to trigger RSVP resends

5. Timezones

  • Use Case: Global teams.
$event = new Event();
$event->setDateStart(\DateTime::createFromFormat('Y-m-d H:i:s', '2024-05-20 10:00:00', new \DateTimeZone('America/New_York')));
$event->setTimeZone(new Property\TimeZone('America/New_York'));

Integration Tips

Laravel-Specific Patterns

  1. Artisan Command for iCal Export

    namespace App\Console\Commands;
    
    use Eluceo\iCal\Component\Calendar;
    use Illuminate\Console\Command;
    
    class ExportEventsCommand extends Command
    {
        protected $signature = 'events:export';
        protected $description = 'Export events to iCal';
    
        public function handle()
        {
            $calendar = new Calendar();
            // ... add events ...
            $this->info('iCal file generated: ' . file_put_contents(storage_path('app/events.ics'), $calendar->render()));
        }
    }
    
  2. API Response with iCal Attachment

    use Symfony\Component\HttpFoundation\StreamedResponse;
    
    return new StreamedResponse(
        function () {
            http_response_code(200);
            header('Content-Type: text/calendar');
            header('Content-Disposition: attachment; filename="events.ics"');
            echo (new Calendar())->render();
        },
        200
    );
    
  3. Storing iCal in Database

    • Serialize the rendered string and store in a LongText column:
    $eventModel->ical_data = $calendar->render();
    $eventModel->save();
    
  4. Validation

    • Use Laravel’s validation to ensure required fields before creating iCal components:
    $validated = $request->validate([
        'title' => 'required|string',
        'start_time' => 'required|date',
        'end_time' => 'required|date|after:start_time',
    ]);
    

Gotchas and Tips

Pitfalls

  1. Timezone Handling

    • Issue: Events may appear misaligned if timezones aren’t set explicitly.
    • Fix: Always attach a TimeZone property or use UTC for consistency.
    $event->setTimeZone(new Property\TimeZone('UTC'));
    
  2. UUID Collisions

    • Issue: Duplicate UUIDs can cause conflicts in clients (e.g., Outlook).
    • Fix: Use a unique generator (e.g., Ramsey\Uuid):
    use Ramsey\Uuid\Uuid;
    
    $event->setUuid(Uuid::uuid4()->toString());
    
  3. Recurrence Rule Edge Cases

    • Issue: Complex RRULE (e.g., EXDATE, RDATE) may not render as expected.
    • Fix: Test with icalendar.org’s validator or use the Eluceo\iCal\Property\RRule constructor carefully.
    • Tip: Start simple (e.g., FREQ=WEEKLY) before adding exceptions.
  4. Property Ordering

    • Issue: Some clients (e.g., Apple Calendar) expect specific property ordering.
    • Fix: Manually order critical properties (e.g., DTSTART before SUMMARY):
    $event->setDateStart($startTime);
    $event->setName($title);
    
  5. Character Encoding

    • Issue: Non-ASCII characters (e.g., é, ü) may corrupt the .ics file.
    • Fix: Encode properties explicitly:
    $event->setDescription(mb_convert_encoding($description, 'UTF-8'));
    
  6. Memory Limits

    • Issue: Large calendars (e.g., 10,000+ events) may hit PHP’s memory limit.
    • Fix: Stream the output or process in batches:
    $calendar = new Calendar();
    foreach ($events->chunk(1000) as $chunk) {
        foreach ($chunk as $event) {
            $calendar->addComponent($event);
        }
        file_put_contents('events_part_' . $i++ . '.ics', $calendar->render());
    }
    

Debugging Tips

  1. Validate Output
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.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui