dflydev/fig-cookies
PSR-7 cookie helper for managing Cookie request headers and Set-Cookie response headers. Provides Cookies and SetCookies collections to read from requests/responses, modify cookie values/attributes, and render updated headers back into PSR-7 messages.
symfony/http-foundation and psr/http-message).collect(), tap()), reducing side effects.FigRequestCookies and FigResponseCookies simplify cookie manipulation, reducing boilerplate in middleware, controllers, and services.HandleIncomingCookies, SetOutgoingCookies) to centralize cookie logic.$request->cookie() or $response->cookie() calls with a more structured API.Psr15Middleware facade) for HTTP toolkit compatibility.Cookie/SetCookie instances).declare(strict_types=1) for full type safety (Laravel 8+ supports this natively).Cookie, SetCookie) for performance-critical paths.Cookies/SetCookies instances in middleware.Request/Response may not expose raw headers identically to PSR-7. Verify compatibility with:
$request->getHeaders()['cookie'] // vs PSR-7 $request->getHeader('Cookie')
Middleware vs. Facade Tradeoffs:
AuthenticateViaCookie) or be scattered via facades in controllers?Cookie Serialization:
symfony/serializer or Laravel’s encrypt() for sensitive data.SameSite/Partitioned Attributes:
SameSite and Partitioned attributes. The package supports these, but ensure:
Response correctly renders headers (test with ->getHeaders()).ShareErrorsFromSession).Legacy Code:
$request->cookie() calls transition to FigRequestCookies::get()?trait LegacyCookieCompatibility {
public function cookie($name, $default = null) {
return FigRequestCookies::get($this, $name, $default)->getValue();
}
}
Performance Benchmarking:
laravel-debugbar to compare execution time.Illuminate\Http\Request (PSR-7 compatible via symfony/http-foundation).Illuminate\Http\Response (PSR-7 via symfony/http-foundation).Psr15Middleware facade.Http client (for outgoing cookies).pestphp/phpunit (mock Cookie/SetCookie).laravel-pint/php-cs-fixer (immutable objects enforce consistency).Phase 1: Middleware Integration
FigRequestCookies:
// Before
$token = $request->cookie('auth_token');
// After
$token = FigRequestCookies::get($request, 'auth_token')->getValue();
SetCookie logic to response middleware:
$response = FigResponseCookies::set($response, SetCookie::create('user_prefs')
->withValue(json_encode($prefs))
->withMaxAge(3600)
);
Phase 2: Controller/Service Layer
$request->cookie() with facades in controllers:
public function updateTheme(Request $request) {
$theme = FigRequestCookies::get($request, 'theme', 'default')->getValue();
// ...
}
modify() for dynamic updates:
$request = FigRequestCookies::modify($request, 'visits', fn(Cookie $c) =>
$c->withValue($c->getValue() + 1)
);
Phase 3: Performance Optimization
// Before (facade)
$cookies = FigRequestCookies::getAll($request);
// After (primitive)
$cookies = Cookies::fromRequest($request);
symfony/http-foundation bridge for PSR-7.psr/http-message (Laravel includes this via illuminate/http).cookie() helper (they operate at different layers).SameSite/Partitioned attributes (critical for GDPR/CCPA).SameSite=None is required).composer.json:
composer require dflydev/fig-cookies
config/app.php:
Illuminate\Foundation\Providers\FoundationServiceProvider is loaded (for PSR-7).CookieMigrationMiddleware to handle legacy $request->cookie() calls.SameSite conflicts.tideways/xhprof or blackfire.io.Request/Response as immutable).phpstan to enforce type safety.phpcs rules to detect anti-patterns (e.g., excessive facade usage).dumpCookies() helper:
function dumpCookies(Request $request) {
dd(Cookies::fromRequest($request)->all());
}
Set-Cookie headers:
$response->getHeaders()['set-cookie'];
strtolower().curl -H "Cookie: ..." and browser dev tools.How can I help you explore Laravel packages today?