- How do I integrate dflydev/fig-cookies into a Laravel middleware for handling incoming cookies?
- Use `FigRequestCookies::fromRequest($request)` to parse cookies into immutable `Cookie` objects. Store them in middleware for later use, like authentication tokens. Example: `$token = FigRequestCookies::get($request, 'auth_token')->getValue();`
- Can this package replace Laravel’s built-in `$request->cookie()` method in controllers?
- Yes, but consider facades for business logic and middleware for cross-cutting concerns. For migration, wrap legacy calls with a trait like `LegacyCookieCompatibility` to reuse existing code while adopting the new API.
- Does dflydev/fig-cookies support SameSite and Partitioned cookie attributes for modern browsers?
- Yes, the package supports these attributes via `SetCookie::create()` methods. Ensure Laravel’s `Response` correctly renders headers by testing with `$response->getHeaders()` after setting cookies.
- Will this package work with Laravel’s PSR-15 middleware (e.g., Psr15Middleware facade)?
- Absolutely. The package is PSR-7 compliant and integrates seamlessly with Laravel’s PSR-15 middleware stack. Use `FigResponseCookies` to modify responses in middleware before they’re sent to the client.
- How do I handle encrypted or serialized cookie values (e.g., JSON) with this package?
- The package doesn’t enforce serialization, so pair it with Laravel’s `encrypt()` or `symfony/serializer` for sensitive data. Example: `SetCookie::create('user_prefs', encrypt($data))->withExpires(...)`.
- Is there a performance overhead compared to manually parsing `$_COOKIE` or `$request->cookie()`?
- Minimal overhead due to immutable objects, but facades add slight abstraction. For performance-critical paths, use primitives like `Cookie::fromString($cookieHeader)` directly. Benchmark with `laravel-debugbar` if needed.
- Does this package work with Laravel’s HTTP client (e.g., sending cookies in outgoing requests)?
- Yes, if your HTTP client uses PSR-7 messages (like Guzzle with PSR-18). Use `SetCookies::fromResponse()` to extract cookies from responses and `Cookies::renderIntoCookieHeader()` to attach them to outgoing requests.
- How do I test cookie logic with dflydev/fig-cookies in PHPUnit or Pest?
- Mock `Cookie` and `SetCookie` objects easily due to their immutable nature. Example: `$mockCookie = $this->createMock(Cookie::class); $mockCookie->method('getValue')->willReturn('test');` Then inject into `Cookies` collections for testing.
- What Laravel versions are supported, and is PHP 8.x compatible?
- The package requires PHP 7.4+ (Laravel 8+) for strict typing. It’s compatible with Laravel 8/9/10 and PSR-7 implementations like `symfony/http-foundation`. No PHP 8.x-specific features are required.
- Are there alternatives to dflydev/fig-cookies for PSR-7 cookie handling in Laravel?
- Laravel’s native `Request` and `Response` methods work, but this package offers a structured, immutable API for complex cookie logic. Alternatives like `league/cookie` exist but lack Laravel/PSR-7 integration. Choose this for middleware-centric or stateless API workflows.