- How do I install and set up **protobuf-php** in a Laravel project?
- Run `composer require centraldesktop/protobuf-php` to install the package. You’ll also need Google’s `protoc` compiler (v2.3+) and PEAR’s `Console_CommandLine`. For Laravel, ensure PHP 7.4+ is used (PHP 5.3 is deprecated). Use the `protoc` plugin to generate PHP classes from your `.proto` files.
- Can I use this package to replace JSON/XML serialization in Laravel APIs?
- Yes. The package supports multiple codecs (binary, JSON, XML, etc.), so you can serialize/deserialize Eloquent models or API responses efficiently. For APIs, use the binary codec for smaller payloads and faster parsing, or JSON for compatibility with clients.
- Does **protobuf-php** support Laravel’s Eloquent models or database storage?
- You can serialize Eloquent models to binary (e.g., for Redis or database blobs), but there’s no built-in migration tool for `.proto` schema changes. Manually sync schema updates and validate data before storage to avoid corruption.
- What Laravel versions and PHP versions does this package support?
- The package officially supports PHP 5.3+, but Laravel 8+ requires PHP 7.4+. Upgrade to PHP 7.4+ for modern Laravel versions. Test integer/float handling if migrating from older PHP, as precision quirks may require validation layers.
- How do I handle large protobuf messages in Laravel (e.g., >10MB)?
- The package loads entire messages into memory, which may cause issues with large payloads. Benchmark your use case and consider chunking data or streaming alternatives (e.g., gRPC for PHP). For now, avoid payloads exceeding ~10MB without optimization.
- Can I generate PHP classes from `.proto` files without running `protoc`?
- No, the `protoc` compiler is required to generate PHP classes. However, the package supports dynamic messages via reflection, so you can work with protobuf data without code generation for simple use cases.
- What are the alternatives to **protobuf-php** for Laravel?
- Consider `php-protobuf` (active, PHP 8+ support, better streaming) or `MessagePack` (simpler, lighter, but less feature-rich). For JSON-focused apps, stick with Laravel’s built-in serialization, but protobuf offers superior performance for binary data.
- How do I customize the generated PHP classes from `.proto` files?
- The package uses template-based code generation, so you can override templates to customize generated classes. Check the [README](https://github.com/centraldesktop/Protobuf-PHP) for details on extending or modifying the output.
- Does this package support gRPC or streaming RPC in Laravel?
- No, this package focuses on protobuf serialization and message generation, not gRPC. For streaming RPC, consider integrating with a gRPC library like `php-grpc` or using binary protobuf over HTTP with custom middleware.
- How do I validate protobuf data in Laravel (e.g., from untrusted sources)?
- Use Laravel’s validation rules or custom logic to check protobuf data before processing. The package supports reflection, so you can inspect dynamic messages for required fields or value ranges. For critical data, combine with schema validation tools.