- Can I use datto/json-rpc to create a Laravel API with JSON-RPC 2.0 endpoints?
- Yes, but you’ll need an additional transport layer like `datto/json-rpc-http` for HTTP-based APIs. The core package handles message parsing and validation, while the HTTP package integrates with Laravel’s routing system. Pair it with middleware for auth or rate limiting.
- How do I integrate JSON-RPC with Laravel’s queue system for async processing?
- Extend Laravel’s `ShouldQueue` trait in a custom job class and use the JSON-RPC client to dispatch requests. For example, create a `JsonRpcJob` that processes RPC calls asynchronously. The core library handles message serialization, while queues manage execution.
- Does this package support Laravel’s middleware pipeline for JSON-RPC requests?
- Yes, you can create a custom middleware to parse and validate incoming JSON-RPC requests before they reach your routes. Use Laravel’s `Handle` interface to intercept requests, decode them with the JSON-RPC server, and pass the result to your controllers or services.
- What Laravel versions are compatible with datto/json-rpc?
- The package supports PHP 7.0+ and works with Laravel 5.8+. For optimal performance, use PHP 7.1+ with Laravel 6.x or later. It’s framework-agnostic but integrates seamlessly with Lumen and Laravel’s full stack.
- How do I secure JSON-RPC endpoints in Laravel (e.g., API tokens or Sanctum)?
- Use Laravel’s built-in auth middleware (e.g., `auth:api`) or Sanctum to protect RPC endpoints. The JSON-RPC server processes requests after auth, so wrap your routes or middleware around the RPC handler. For example, apply `auth:api` to routes handling RPC calls.
- Can I expose Eloquent models as JSON-RPC methods (e.g., `User::find()`)?
- Absolutely. Register Eloquent methods as RPC endpoints by defining them in your `Api` class (e.g., `$api->addMethod('user.find', [UserService::class, 'find'])`). The library will serialize responses automatically, and you can customize output with preEncode/postDecode hooks.
- What’s the performance impact of JSON-RPC in Laravel compared to REST?
- JSON-RPC adds serialization/deserialization overhead, but it’s minimal for most use cases. Benchmark with your specific payloads, but expect ~10–30ms extra latency per request. For high-throughput systems, consider batching RPC calls or caching frequent responses with Laravel’s `Cache` facade.
- How do I handle errors in JSON-RPC responses to match Laravel’s ProblemDetails format?
- Map JSON-RPC errors to Laravel’s `ProblemDetails` by extending the `Server` class and overriding the `handleError` method. Convert exceptions (e.g., `ApplicationException`) to structured responses with `status`, `title`, and `detail` fields before returning them to the client.
- Are there alternatives to datto/json-rpc for Laravel JSON-RPC implementations?
- Yes, consider `rectorphp/json-rpc` for a more opinionated Laravel integration or `php-json-rpc-server` for a full-stack solution. However, `datto/json-rpc` is transport-agnostic and fully compliant with the 2.0 spec, making it ideal for custom protocols or legacy systems.
- How do I test JSON-RPC endpoints in Laravel with PHPUnit?
- Mock the JSON-RPC client/server in your tests using PHPUnit’s `createMock`. Test edge cases like malformed requests, timeouts, or invalid method calls by injecting fake responses. The package’s 100% unit-test coverage ensures reliability, but validate integration with Laravel’s HTTP layer separately.