- How does boson-php/http-contracts differ from Laravel’s built-in FormRequest validation?
- This package provides reusable, declarative HTTP contracts (e.g., for requests, responses, and middleware) that can enforce stricter validation rules or schemas across your API. Unlike FormRequest, which is tightly coupled to Laravel’s validation system, these contracts are implementation-agnostic, making them easier to mock in tests or swap libraries. For example, you could define a `UserContract` for API responses that enforces JSON:API or OpenAPI schemas, then reuse it across microservices or third-party integrations.
- Can I use boson-php/http-contracts with Laravel’s middleware pipeline?
- Yes, the package is designed to integrate with Laravel’s middleware pipeline. Contracts can be bound to routes or middleware groups just like native Laravel middleware. For instance, you could enforce a `JWTContract` in the `auth:api` middleware group to validate incoming requests before they reach your controllers. The package likely provides interfaces for middleware contracts that align with Laravel’s `$middleware` or `$routeMiddleware` arrays.
- Does boson-php/http-contracts support Laravel 10 and PHP 8.4+?
- The package explicitly requires PHP 8.4+ and is compatible with modern Laravel versions (including 10.x). However, since it’s part of the Boson ecosystem, ensure your Laravel app isn’t using deprecated features or Symfony components that conflict with Boson’s contracts. Always check the [Boson documentation](https://bosonphp.com/doc) for Laravel-specific compatibility notes, as the package may rely on Boson’s underlying architecture.
- How do I migrate from Laravel’s FormRequest to boson-php/http-contracts?
- Start by identifying repetitive validation logic in your FormRequest classes. Replace them with contracts defined in this package, which can enforce stricter schemas or nested validation. For example, convert a `StoreUserRequest` into a `UserContract` that validates both the request body and response structure. Use Laravel’s service container to bind contracts to your routes or middleware, then gradually replace FormRequest validation with contract enforcement. Keep a fallback using feature flags during the transition.
- Will boson-php/http-contracts work with Lumen or Laravel’s API resources?
- Yes, the package is designed for API-heavy applications, including Lumen and Laravel’s API resources. You can use contracts to define consistent request/response schemas for API resources, ensuring responses adhere to a standardized format (e.g., JSON:API or GraphQL). For Lumen, bind contracts to routes or middleware just as you would in Laravel. The package’s focus on decoupling makes it ideal for microservices or headless APIs where strict contracts are critical.
- Are there performance overhead concerns with boson-php/http-contracts?
- The package is lightweight and focuses on runtime validation and type safety rather than heavy processing. However, if you enforce complex nested validation or serialization rules, there may be a minor overhead compared to native Laravel responses. Benchmark your API endpoints before and after integration to assess the impact. The package’s design prioritizes clarity and reusability, so performance trade-offs are typically justified for large-scale APIs or microservices.
- How do I test HTTP contracts in Laravel with Pest or PHPUnit?
- Contracts in this package are designed to be easily mockable, making them ideal for unit and integration tests. Use Laravel’s testing helpers (e.g., `actingAs()`, `json()`, or `assertJsonStructure()`) alongside mocked contracts to validate request/response behavior. For example, you could mock a `PaymentContract` to test how your service handles invalid payment data without hitting external APIs. The package’s documentation should include examples for testing middleware or route contracts.
- Can boson-php/http-contracts replace Symfony’s Contracts or JSON Schema validation?
- This package offers a different approach than Symfony’s Contracts or JSON Schema validators. While those tools focus on runtime type checking or schema validation, boson-php/http-contracts provides a higher-level abstraction for HTTP-specific concerns (e.g., request/response DTOs, middleware contracts). If you’re already using Symfony’s Contracts, you might find this package complementary for API-layer concerns. For JSON Schema, compare whether you need runtime validation (Schema) or declarative contracts (Boson) for your use case.
- Does boson-php/http-contracts integrate with Laravel Sanctum or Passport for auth?
- Yes, the package can work alongside Sanctum or Passport by defining auth-specific contracts (e.g., `JWTContract` or `OAuthContract`). You could enforce token validation rules or claim structures using contracts, then bind them to middleware like `auth:sanctum` or `auth:api`. This ensures consistency across your auth layer while leveraging Laravel’s existing auth systems. Check the package’s documentation for examples of integrating with Sanctum/Passport’s middleware or guards.
- What’s the maintenance burden of adopting boson-php/http-contracts in a large Laravel app?
- The initial burden depends on how deeply you integrate contracts into your codebase. Start with a pilot project (e.g., a single API endpoint) to test the package’s fit. If you’re using Boson’s ecosystem (e.g., for microservices), the transition may be smoother. For large apps, plan for incremental migration: replace FormRequest classes first, then API resources, and finally middleware. The package’s MIT license and active test suite suggest low long-term risk, but monitor the Boson project’s roadmap for breaking changes.