Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Http Cookie Laravel Package

httpsoft/http-cookie

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require httpsoft/http-cookie
    

    Requires PSR-7 HTTP message interfaces (e.g., zendframework/zend-diactoros or slim/psr7).

  2. First Use Case: Setting a Cookie

    use HttpSoft\Cookie\Cookie;
    use HttpSoft\Cookie\CookieJar;
    
    $cookie = new Cookie('user_token', 'abc123', 3600, '/', 'example.com', true, true);
    $jar = new CookieJar();
    $jar->setCookie($cookie);
    
  3. Where to Look First


Implementation Patterns

Core Workflows

  1. Cookie Management in Middleware

    use Psr\Http\Message\ResponseInterface;
    use Psr\Http\Message\ServerRequestInterface;
    use Psr\Http\Server\MiddlewareInterface;
    
    class CookieMiddleware implements MiddlewareInterface {
        public function process(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface {
            $jar = new CookieJar();
            $jar->loadFromRequest($request); // Parse cookies from request
            $response = $jar->applyToResponse($response); // Apply cookies to response
            return $response;
        }
    }
    
  2. Laravel Integration (Request/Response)

    // In a Laravel service provider or middleware:
    $jar = new CookieJar();
    $jar->loadFromRequest($request); // Parse cookies from Laravel's `Request` object
    $response = $jar->applyToResponse($response); // Apply cookies to Laravel's `Response` object
    
  3. Cookie Serialization

    $cookie = new Cookie('prefs', json_encode(['theme' => 'dark']), 86400);
    $jar->setCookie($cookie);
    

Integration Tips

  • PSR-7 Compatibility: Use with frameworks like Slim, Lumen, or custom PSR-7 apps.
  • Laravel-Specific: Extend Illuminate\Http\Request/Response by wrapping CookieJar in a trait or facade.
  • Testing: Mock CookieJar for unit tests by injecting it into services.

Gotchas and Tips

Pitfalls

  1. Domain/Path Mismatch

    • Cookies set with example.com won’t work on sub.example.com unless explicitly allowed.
    • Fix: Use setDomain('.example.com') for subdomains.
  2. Secure/HTTP-Only Flags

    • Forgetting secure: true on HTTPS sites or httponly: true for security risks.
    • Fix: Always set flags in production:
      $cookie = new Cookie('session', $value, 0, '/', 'example.com', true, true);
      
  3. PSR-7 Message Quirks

    • Some PSR-7 implementations (e.g., zend-diactoros) require explicit cookie header parsing.
    • Fix: Use CookieJar::loadFromRequest() to avoid manual parsing.

Debugging

  • Cookie Not Setting?

    • Check Set-Cookie headers in browser dev tools or curl -v.
    • Verify Domain/Path match the request URL.
  • Cookie Overwriting

    • CookieJar merges cookies; explicit setCookie() overwrites.
    • Fix: Use addCookie() to append instead of replace.

Extension Points

  1. Custom Cookie Classes Extend HttpSoft\Cookie\Cookie to add metadata (e.g., SameSite attributes):

    class ExtendedCookie extends Cookie {
        public function __construct(...$args) {
            parent::__construct(...$args);
            $this->setSameSite('Strict'); // Not in base class
        }
    }
    
  2. Middleware Decorators Wrap CookieJar in middleware to add logic (e.g., auto-expiry for guest users):

    $jar = new CookieJar();
    $jar->setCookie(new Cookie('guest', 'true', 300)); // 5-minute expiry
    
  3. Laravel Facade Create a facade for CookieJar to integrate seamlessly:

    // app/Facades/Cookie.php
    namespace App\Facades;
    use Illuminate\Support\Facades\Facade;
    class Cookie extends Facade {
        protected static function getFacadeAccessor() { return 'cookie.jar'; }
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky