- How does CBOR compare to JSON in Laravel APIs for payload size and speed?
- CBOR reduces payload sizes by 30–70% compared to JSON, drastically improving API latency and bandwidth. Benchmarks show it’s 2–5x faster for large datasets (e.g., IoT telemetry or complex nested objects). For Laravel, replace JSON middleware with CBOR-aware middleware to see immediate gains in response times and storage efficiency.
- Can I use this package with Laravel’s Eloquent models for database storage?
- Direct database storage isn’t supported out of the box, but you can serialize Eloquent model attributes to CBOR for caching (e.g., Redis) or API responses. For database queries, you’d need custom adapters to encode/decode CBOR in query builders or use it as a binary field type. Focus on high-impact areas like API payloads first.
- What Laravel versions and PHP requirements does this package support?
- The package requires PHP 8.0+, which aligns with Laravel 8+ (Laravel 10+ needs PHP 8.1+). It’s fully compatible with modern Laravel versions and won’t conflict with core dependencies. Optional extensions like `ext-gmp` or `ext-bcmath` can be enabled via Laravel’s `php.ini` or Docker configurations for numeric performance.
- How do I integrate CBOR into Laravel’s request/response cycle?
- Use middleware to automatically encode responses or decode requests. For example, create a `EncodeResponses` middleware that replaces JSON responses with CBOR by modifying the `Content-Type` header and encoding the payload. Alternatively, use Laravel’s `Response` macros to add CBOR serialization globally.
- Does this package support WebAuthn or COSE for passwordless authentication?
- Yes, it directly supports FIDO2/WebAuthn and COSE standards via CBOR’s native tag system. Integrate it with `laravel/webauthn` to encode authenticator data (e.g., `AuthenticatorAttestationResponse`) in CBOR format, reducing payload sizes and aligning with modern authentication protocols.
- How do I handle large payloads or streaming data with this library?
- The package includes a streaming decoder, ideal for large files or real-time data (e.g., IoT logs or firmware updates). Use `StringStream` or `Stream` objects to decode data incrementally without loading it entirely into memory. This is critical for edge devices or high-throughput APIs.
- Are there any Laravel-specific integrations or packages that extend this library?
- Currently, no official Laravel integrations exist, but you can build custom solutions like Eloquent serializers, Scout search adapters, or API response macros. Contribute back to the library or create internal wrappers for common use cases (e.g., CBOR-aware caching or queue jobs). Start with high-impact areas like APIs.
- What’s the learning curve for developers new to CBOR?
- CBOR’s type system (major types 0–7, tags) may require familiarization, but the package’s API is type-safe and well-documented. Start with built-in tags (e.g., timestamps, URIs) and migrate from JSON incrementally. The documentation includes guides on custom tags and integration examples to ease adoption.
- How do I enable optional extensions like `ext-gmp` or `ext-bcmath` in Laravel?
- Add the extensions to your Laravel environment via `php.ini` or Docker configurations. For example, in a Dockerfile, include `RUN docker-php-ext-install gmp bcmath`. These extensions improve performance for large integers or decimal fractions but aren’t required for basic usage.
- What alternatives to CBOR exist for binary serialization in Laravel?
- Alternatives include MessagePack (via `msgpack/msgpack`), Protocol Buffers (via `php-protobuf`), or JSON with gzip compression. CBOR stands out for its RFC compliance, built-in tags (e.g., for WebAuthn/COSE), and streaming support. Choose CBOR if you need compact binary format with extensibility; MessagePack is simpler but lacks tags.