sabre/dav
sabre/dav is a popular PHP framework for building WebDAV, CalDAV, and CardDAV servers. It provides the core server stack plus extensions for calendars and contacts, with full documentation and installation guides at sabre.io.
Install sabre/dav via Composer:
composer require sabre/dav
Begin with a minimal working WebDAV server using the included filesystem backend. Create server.php:
<?php
require 'vendor/autoload.php';
use Sabre\DAV;
use Sabre\DAVACL;
$server = new DAV\Server(
new DAV\FS\Directory('/path/to/webdav/root')
);
$server->setBaseUri('/webdav/');
// Enable basic ACL support (optional but recommended)
$server->addPlugin(new ACL\Simple\ACL([
'private' => ['{DAV:}read', '{DAV:}write'],
'public' => ['{DAV:}read'],
]));
$server->exec();
Run with PHP's built-in server:
php -S localhost:8000 server.php
First use case: verify access via browser at http://localhost:8000/webdav/ or using cadaver CLI tool:
cadaver http://localhost:8000/webdav/
Check documentation at https://sabre.io/dav/ — especially the Getting Started and Server chapters.
Tree abstraction: All data (files, calendars, contacts) is modeled as a tree of INode implementations. Extend Node classes (Directory, File, Collection) or implement INode for custom storage.
Custom backend integration: Implement Backend\BackendInterface (for CardDAV/CalDAV) or extend Backend\AbstractBackend. Use PDO backends as reference (see sabre/dav/src/CalDAV/Backend/PDO.php). For high-performance, prefer iterators over arrays in getChildren().
Event-driven architecture: Leverage Sabre’s event system for hooks:
$server->on('beforeMethod', function ($methodName, $path) {
if ($methodName === 'PROPFIND') {
// Log property requests
}
});
$server->on('afterGetProperties', function ($path, $properties, $outputProperties) {
unset($outputProperties['{http://example.org}secret']);
});
Plugins: Extend PluginInterface to add features (e.g., custom headers, auth, sync). Key built-in plugins:
ACLPlugin — access controlCalDAV\Plugin — calendar scheduling, iTIP messagesCardDAV\Plugin — vCard imports/exports, addressbook-queryBrowser — human-readable listingLocks — file locking (requires Lock\BackendInterface)Sync workflows: For incremental sync (CalDAV/CardDAV), implement getChanges() in your backend and use Sync\SyncPlugin. Support sync-token and sync-level REPORTs.
Integration tip: Wrap Sabre\DAV\Server in a Laravel controller or middleware. Use route groups for WebDAV paths (e.g., /dav/{user}). Inject auth via middleware and set $server->setBaseUri() dynamically.
Node path handling: Paths must be absolute and normalized (no .., leading /). The Tree resolves paths using getNodeForPath(). Override it for custom routing (e.g., per-user namespaces like /users/jane/). Caching here (getNodeForPath() caching in 4.7.0+) is critical for performance.
PHP 8.0+ deprecations: Ensure no implicit resource-to-string conversions. Common fixes: cast resources to strings (e.g., (string)$resource), and avoid gettype() comparisons — use is_*().
PROPFIND gotcha: When returning propstat elements, only one of {DAV:}prop or {DAV:}status per propstat. Multiple props in one response must be grouped under one prop.
Locks implementation: Locks require Lock\BackendInterface. Lock\Backend\AbstractBackend provides a DB-based implementation using MySQL/Postgres. Test with litmus test suite — locks and basic suites are critical.
CalDAV iTIP: To enable calendar invitations (e.g., meeting requests), configure IMipPlugin with a mailer (e.g., Symfony Mailer or SwiftMailer) and enable scheduling ($server->addPlugin(new CalDAV\Scheduling\Plugin())).
CardDAV filters: addressbook-query uses filter XML (e.g., {urn:ietf:params:xml:ns:carddav}address-data-filter). Avoid complex regex — use Sabre’s FilterParser where possible.
Extensibility: Override Sabre\DAV\PropFind methods like propFindUnfiltered() (4.6.0+) to bypass property filtering when needed. Use beforeMethod:* and method:* wildcards for events — 4.0+ dropped beforeMethod/method without wildcard.
Debug tip: Enable debug mode:
$server->on('exception', function ($e) {
error_log($e->getTraceAsString());
});
$server->setDebugLevel(2); // Enable verbose logging
Use Server::addPlugin(new DAV\Browser\Plugin()) for browser GUI inspect requests/responses.
How can I help you explore Laravel packages today?