Installation
composer require eluceo/ical
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();
Where to Look First
src/ directory for classes like Calendar, Event, Todo, Journal, and their properties.tests/ folder for practical usage examples (e.g., EventTest.php).Eluceo\iCal\Property for customizing event attributes (e.g., Attendee, Location, Categories).$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());
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);
.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());
}
}
$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
$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'));
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()));
}
}
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
);
Storing iCal in Database
LongText column:$eventModel->ical_data = $calendar->render();
$eventModel->save();
Validation
$validated = $request->validate([
'title' => 'required|string',
'start_time' => 'required|date',
'end_time' => 'required|date|after:start_time',
]);
Timezone Handling
TimeZone property or use UTC for consistency.$event->setTimeZone(new Property\TimeZone('UTC'));
UUID Collisions
Ramsey\Uuid):use Ramsey\Uuid\Uuid;
$event->setUuid(Uuid::uuid4()->toString());
Recurrence Rule Edge Cases
RRULE (e.g., EXDATE, RDATE) may not render as expected.Eluceo\iCal\Property\RRule constructor carefully.FREQ=WEEKLY) before adding exceptions.Property Ordering
DTSTART before SUMMARY):$event->setDateStart($startTime);
$event->setName($title);
Character Encoding
é, ü) may corrupt the .ics file.$event->setDescription(mb_convert_encoding($description, 'UTF-8'));
Memory Limits
$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());
}
How can I help you explore Laravel packages today?